1

I'm using GSON for parsing. I'm able to parse json using arrayName but I don't want to use arrayName as it is not static.arrayName can be change and more will be added from server. Please suggest.

JSON:

{
    "cityCode": "",
    "list": {
        "one": [
            {
                "adults": 2
            },
            {
                "adults": 2
            },
            {
                "adults": 2
            }
        ],
        "three": [
            {
                "adults": 2
            },
            {
                "adults": 2
            },
            {
                "adults": 2
            },
            {
                "adults": 2
            },
            {
                "adults": 2
            }
        ]
    }
}
1
  • Your Json is very bad. You should Json 1st according to your requirement. Commented Jul 1, 2015 at 7:15

2 Answers 2

1

This works for your case. Remember that JSONObject has keys() method which list all its attributes. And you can see that, Iterator result is un-ordered, run this code and see result.

static void parseJson(String json){
    try{
        JSONObject object = new JSONObject(json);

        //Get "list" JSONObject
        JSONObject obj = object.getJSONObject("list");
        //Try to get all attributes of that object
        @SuppressWarnings("unchecked")
        Iterator<String> iterator = obj.keys();
        while (iterator.hasNext()) {
            String key = iterator.next();
            JSONArray arr = obj.getJSONArray(key);
            int size = arr.length();
            for(int i = 0; i < size; i++){
                JSONObject o = arr.getJSONObject(i);
                Log.e("JSON", "=> "+o.getInt("adults"));
            }
        }
    }catch(JSONException e){
        e.printStackTrace();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the code but I'm using Gson so how can be done using json pojo
0

Your Json should look like following.

{
    "cityCode": "",
    "list":[
            {
                "index":"one"
                "adults": 2
            },
            {
                "index":"one"
                "adults": 2
            },
            {
                "index":"one"
                "adults": 2
            },
            {
                "index":"three"
                "adults": 2
            },
            {
                "index":"three"
                "adults": 2
            },
            {
                "index":"three"
                "adults": 2
            },
            {
                "index":"three"
                "adults": 2
            },
            {
                "index":"three"
                "adults": 2
            }
        ]
}

Share you server side code.

2 Comments

I agrree with u but I cannot change json format so is there any possibilities to parse it wihtout using array Name
Not Possible without changing format. because you are not sure about total number of static indexes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.