0

I found a lot of tutorials here, how to parse JSON Data of an JSON Array.

But my JSON File is a little bit complicate (for me). It has the following structure:

JSON File (excerpt)

{
    "data": {
        "schedule_id": {
            "12": {
                "name": "CP",
                "d_id": [
                    "7"
                ]
            },

            "17": {
                "name": "WT",
                "d_id": [
                    "88",
                    "14"
                ]
            }
        }
    }
}

Java Code (excerpt)

Info: I've parsed the json into "json" using HTTP GET in another Activity.

JSONObject dataJsonData = json.getJSONObject("data").getJSONObject("schedule_id");

Now I would parse through the ids using a "for"-loop:

ArrayList<String> parsedNameList = new ArrayList<String>();

for (int i = 0; i < idontknow; i++) {
    String s = new Integer(i).toString();
    parsedNameList.add(dateJsonData.getJSONObject(i).getString("name"));
}

This would add each value of "name" to the ArrayList.

But there are 2 problems: 1. The "schedule_id"s are messed up and incomplete. For example, there is no id "0" and, like in given json, the ids "13, 14, 15, 16" are missing. 2. The "schedule_id"s will be changed every day and will be mixed.

So I don't think, that I can use the predefined integer "i" because some integers aren't a "schedule_id". I could use this loop and would ignore empty entries in the ArrayList, but the JSON contains more than 200 ids - I think it would be more efficient, if there is another way to parse through this json.

I found some informations of the getJSONArray method, but the "d_id"s are Arrays - not the "schedule_ids".

Does anyone has an idea? Is there maybe a placeholder for the parameter of the getString method?

PS: Excuse my english, I'm from germany :)

2 Answers2

0

I think this should work

Iterator keys = dataJsonData.keys();

while(keys.hasNext()) {
    // loop to get the dynamic key
    String currentDynamicKey = (String)keys.next();

    // get the value of the dynamic key
    String currentDynamicValue = dataJsonData .getString(currentDynamicKey);
    parsedJsonList.add(currentDynamicValue );
}

Source: How to parse a dynamic JSON key in a Nested JSON result?

Community
  • 1
  • 1
Sushant
  • 1,834
  • 19
  • 32
0

According to your context, it is better to change the json structure,if you have access to web service.

Request for json structure to be like this,

 {
    "data":{
        "schedule":[
            {
                "id":12,
                "name":"CP",
                "d_id":[
                    "7"
                ]
            },
            {
                "id":12,
                "name":"CP",
                "d_id":[
                    "7",
                    "88"
                ]
            },
            {
                "id":200,
                "name":"AT",
                "d_id":[
                    "7",
                    "88"
                ]
            }
        ]
    }
}

Otherwise too much iteration can slow down you CPU.

guness
  • 6,336
  • 7
  • 59
  • 88
user3308752
  • 74
  • 1
  • 8