-2

I have json object with this structure.

{
    "count": 3,
    "result": {
        "1": {
            "brand_id": "1",
            "brand_name": "Adidas",
            "brand_image": "http://a.jpg"
        },
        "2": {
            "brand_id": "2",
            "brand_name": "Asics",
            "brand_image": "http: //b.jpg"
        },
        "3": {
            "brand_id": "3",
            "brand_name": "Adidas adidas",
            "brand_image": "http: //c.jpg"
        }
    }
}

This is not a static one. Count may vary.

I have tried like this. I don't want to use gson. I want to parse in this. How to proceed with such a structure of data.

JSONObject totalData=new JSONObject(results.toString());
JSONObject brandResults=totalData.getJSONObject("result");

// I can get output in this.
Log.e("Result output", brandResults.toString());

// this is not working
int brandCounts = brandResults.getInt("count");
Log.e("BRAND COUNTS", ""+brandCounts);

if (brandCounts > 0) {
    for (int i = 0; i < brandCounts; i++) {
        JSONObject items = brandResults.getJSONObject(Integer.toString(i));
        String brand_id=items.getString("brand_id");
        Log.e("Brand id", brand_id);
    }
}
Squonk
  • 48,735
  • 19
  • 103
  • 135
Star
  • 735
  • 3
  • 13
  • 34
  • Alternatively, `brandResults.length()` – Eric Jan 01 '15 at 13:36
  • don't show duplicate. show me how to solve this. – Star Jan 01 '15 at 13:38
  • @Star: No, that's how stack overflow works. Showing duplicates is the right thing to do, if they are truly mostly similar – Eric Jan 01 '15 at 13:42
  • @ツFellinLovewithAndroidツ : No, this is not a duplicate unless you can find a question along the lines of "I don't know how to correctly parse a JSON string". The link you posted to the "duplicate" doesn't even have any Android-specific code in the question (or any code to be exact) and the answer is generic Java with no Andoid-specific code. As I said, find a duplicate of a question where it is about generic JSON parsing using Java and I'd happily vote to close. I've removed the Android tag from this question and just left the java / json tags. – Squonk Jan 01 '15 at 14:52
  • @Eric: See my comment above. – Squonk Jan 01 '15 at 14:53
  • For the record, i did not mark this as a duplicate – Eric Jan 01 '15 at 21:42

2 Answers2

1

This should answer your question

 int count = totalData.getInt("count");

You are trying to fetch count from the result but your count resides in the JSONObject itself.

Yasmeen
  • 801
  • 1
  • 7
  • 20
0

There is simple mistake when you retrieve the count.Change this in the following way.

JSONObject totalData=new JSONObject(results.toString());
        JSONObject brandResults=totalData.getJSONObject("result");
        Log.e("Result output", brandResults.toString());

        int brandCounts = totalData.getInt("count");

        Log.e("BRAND COUNTS", ""+brandCounts);
        if (brandCounts > 0) {

            for (int i = 1; i <= brandCounts; i++) {
                JSONObject items = brandResults
                        .getJSONObject(Integer.toString(i));
                String brand_id=items.getString("brand_id");
                Log.e("Brand id", brand_id);

            }
        }

Happy Coding

BeingMIAkashs
  • 1,375
  • 11
  • 18