-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);
    }
}
6
  • Alternatively, brandResults.length() Commented Jan 1, 2015 at 13:36
  • don't show duplicate. show me how to solve this. Commented Jan 1, 2015 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 Commented Jan 1, 2015 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. Commented Jan 1, 2015 at 14:52
  • @Eric: See my comment above. Commented Jan 1, 2015 at 14:53

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

this what i mentioned right.
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

2 Comments

i couldn't get brand id.
Oh, I got this you have to start your loop variable "i" from 1. That's why you do not get output. see the edited code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.