0

I'm getting following json from server:

{
"Data": [
    {
        "Record": [
            " d11",
            "d12"
        ]
    },
    {
        "Record": [
            " d21",
            "d22"
        ]
    }
],
"Keys": [
    "Key1",
    " key2"
]

}


I want to retrieve record values which are ordered with respect to keys values(key1, key2?

Note: Using org.json api only.

4
  • First of all, I don't think that JSON is valid. Is the data supposed to be an object rather than a list? And are the keys in the list supposed to match the keys of the data object? Commented Oct 11, 2015 at 10:20
  • Its a valid json.. We have implemented like.. Keys are database columns..and value array(data1 & data2) has d11 d12 & d21 d22 which are respective values of columns.. Each data1 & data2 is individual rows... We've used arraylist to ensure insertion order.. Note: we have to avoid putting key:value pair at server side.. Hence Required to Seek to parse this JSON at client side.. Commented Oct 12, 2015 at 4:46
  • @andersschuller thanks for figuring it out.. I've updated the correct json.. Problem persists.. Commented Oct 12, 2015 at 5:06
  • @andersschuller yes.. Keys in the list are supposed to match keys in data.. Commented Oct 12, 2015 at 5:18

1 Answer 1

1

Your question is still a little unclear to me, but I'm assuming you want to turn that JSON into a list of records, where each record is (for example) a map containing the keys from the list of keys and the values from the data list.

In order to achieve this, we first parse the JSON into a JSONObject:

String json = " ... ";
JSONTokener tokener = new JSONTokener(json);
JSONObject jsonObject = new JSONObject(tokener);

Then we extract the list of keys and the data list:

JSONArray data = jsonObject.getJSONArray("Data");
JSONArray keys = jsonObject.getJSONArray("Keys");

and define a list to contain our output:

List<Map<String, String>> records = new ArrayList<>();

Finally, we iterate over the data list, extract the list of record values for each item in that list, and then iterate over the keys in order to create a map from key to record value:

for (int i = 0; i < data.length(); i++) {
    JSONObject dataItem = data.getJSONObject(i);
    JSONArray recordValues = dataItem.getJSONArray("Record");

    Map<String, String> record = new HashMap<>();

    for (int j = 0; j < keys.length(); j++) {
        String key = keys.getString(j);
        String value = recordValues.getString(j);
        record.put(key, value);
    }

    records.add(record);
}

When we then print the value of records, we get something that looks like:

[{Key1= d11,  key2=d12}, {Key1= d21,  key2=d22}]
Sign up to request clarification or add additional context in comments.

Comments

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.