Skip to content Skip to sidebar Skip to footer

Parse Json In Android

Possible Duplicate: JSON Parsing in Android As I find out I need to use Gson class to parse json to Java object in android. It's quite easy to parse simple varables or array, bu

Solution 1:

      //Model class 

    class Model {

         private  String mId ;
          private   String mScore;

         public Model (String id , String score){

             mId = id ;
             mScore= score
         }


      //getter and setter 

    } 

// in your class

    private ArrayLsit getLsit(String str){
      ArrayLsit<Model > list = new  ArrayLsit<Model>();

        // getting JSON string from URL
        JSONObject json = new JSONObject(str);

        try {
            // Getting Array of Contacts
            contacts = json.getJSONArray("data");

            // looping through All Contacts
            for(int i = 0; i < contacts.length(); i++){
                JSONObject c = contacts.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString("id");
                String score= c.getString("score");
                list.add(new Model(id ,score))

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

   return list

 }

Solution 2:

Check out the code..

        Result = jsonObject.getString("data");

        jsonArray = new JSONArray(Result);
        for (int i = 0; i < jsonArray.length(); i++) {
            jsonObject = jsonArray.getJSONObject(i);
            try {
                jsonObject.getString("Id");

            } catch (Exception ex) {
                cafeandbarsList.add(null);
                ex.printStackTrace();
            }
        }

Thanks


Solution 3:

create to class for json,

Gson gson = new Gson(); Jsondata jsonddata = gson.fromJson(response, Jsondata .class);

===== JsonData.jave

@SerializedName("selected_id")
public String sid;

    @SerializedName("data") 
public List<data> dalalist;

=== data.java

    @SerializedName("id")
public String id;

    @SerializedName("score")
public String score;

Post a Comment for "Parse Json In Android"