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 classModel{

         privateString mId ;
          privateString mScore;

         public Model (String id , String score){

             mId = id ;
             mScore= score
         }


      //getter and setter 

    } 

// in your class

privateArrayLsitgetLsit(String str){
      ArrayLsit<Model > list = newArrayLsit<Model>();

        // getting JSON string from URLJSONObject json = newJSONObject(str);

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

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

                // Storing each json item in variableString id = c.getString("id");
                String score= c.getString("score");
                list.add(newModel(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"