-3

This is the json :

{
"product_id": 1,
"reviews": [
    {
        "comment": "The product is good, I recommend it.",
        "friend": true,
        "friend_type": "friend",
        "like": true,
        "product_id": 1,
        "ratings": {
            "Overall": 3,
            "delivery_time": 3,
            "discounts_and_offers": 3,
            "matches_description": 5,
            "matches_photo": 1,
            "packaging": 3,
            "price": 4
        },
        "reviewer": {
            "age": 36,
            "biography": "This is the biography for Gopika Chadha (8). Xpmaamdkp Wcbmj Gwvc Rvmnthfh Ajluydcsu Iqpsrfl Tyzil Ejzntc Fv Jwuqnoye Anfletfs Uwkkotarm Eyvlugt Zctrgdpn Avck Wwhzzfhg Ao.",
            "connection": "is connected with, has similar likes as and 4 more affinity with you.",
            ,"email": "ggananth+sgepdemo_8_gopika_chadha@gmail.com",
            "friend_type": "friend",
            "id": 8,
            "name": "Gopika Chadha",
            "sex": "female",
            "timestamp": 1472621245.412491
        },
        "self_review": false,
        "title": "It is awesome",
        "usefulness": 10,
        "user_id": 8,
        "viewer_useful": false
    },

and I tried to get the ratings as :

JSONObject obj = new JSONObject(result);
JSONArray reviewsArray=obj.getJSONArray("reviews");
for (int i=0;i<reviewsArray.length();i++) {

            JSONObject reviewsObj = reviewsArray.getJSONObject(i);
            rList.setComment(reviewsObj.optString("comment"));

            rList.setTitle(reviewsObj.optString("title"));
            rList.setUsefulness(reviewsObj.optInt("usefulness"));
            rList.setFriend(reviewsObj.optBoolean("friend"));
            JSONObject ratingsObj=new JSONObject("ratings");
                rList.setOverall(ratingsObj.optInt("Overall"));
                rList.setDeliveryTime(ratingsObj.optInt("delivery_time"));
rList.setDiscountsAndOffers(ratingsObj.optInt("discounts_and_offers"));
rList.setMatchesDescription(ratingsObj.optInt("matches_description"));                   
                rList.setMatchesPhoto(ratingsObj.optInt("matches_photo"));
                rList.setPackaging(ratingsObj.optInt("packaging"));
                rList.setPrice(ratingsObj.optInt("price"));

}

I'm getting till ratings, but I want to get the content inside of ratings like overall, delivery time, discounts, matches description, matches photo, packing and price also I wanted to get the name too.

Abhilash Ravindran C K
  • 1,774
  • 2
  • 11
  • 21
rahulje9
  • 154
  • 9

3 Answers3

1

write below code

JSONObject ratingsObj = reviewsObj.getJsonObject("ratings");

in place of

JSONObject ratingsObj = new JSONObject("ratings");
0

Instead of JSONObject ratingsObj=new JSONObject("ratings"); use JSONObject ratingsObj=reviewsObj.getJsonObject("ratings");

Saurabh Vadhva
  • 624
  • 5
  • 12
0

Replace your code for ratingsObj from

JSONObject ratingsObj = new JSONObject("ratings");

to

JSONObject ratingsObj = reviewsObj.getJsonObject("ratings");

This will work for you.

Chintak Patel
  • 708
  • 5
  • 22