Caroline Koosy
Caroline Koosy

Reputation: 61

How to process a multidimensional json into 3 arrays

I have a JSON result like this:

jArr:
[
  ["53","54","55","62","63","64"],
  ["1133 Budapest Dráva utca 10.","1106 Budapest Örs vezér tere 25.","1106 Budapest Örs vezér tere 25.","1106 Budapest Örs vezér tere 25.","1133 Budapest Dráva utca 10.","1106 Budapest Örs vezér tere 25."],
  [
    ["2016-08-18","15:00:00"],
    ["2016-04-08","13:00:00"],
    ["2016-04-08","14:00:00"],
    ["2016-04-08","10:00:00"],
    ["2016-04-08","13:00:00"],
    ["2016-04-08","15:00:00"]
  ]
]

And I want to process it in java, into 3 array. I want the first [] (like 53, 54...) into int[] id. The second [] into a String[] array and the third is a String too. The 3 arrays are the same size, but the JSON result can be more or less.

The java:

try {
    post.setEntity(new UrlEncodedFormEntity(dataToSend));
    HttpResponse httpResponse = client.execute(post);
    HttpEntity entity = httpResponse.getEntity();
    String result = EntityUtils.toString(entity);
    JSONArray jArr = new JSONArray(result);
    int size = 30;
    String[] dates = new String[size];
    String[] places = new String[size];
    int[] ids = new int[size];
    if (jArr.length() != 0) {
        for (int i = 0; i < jArr.length(); i++){
            //I think it should be processed here
            ids[i] = jArr[0][i].getInt(i); //or something like that, but it doesn't work
            places[i] = jArr[1][i].getSting();    
            dates[i] = jArr[2][i].getSting();    
        }
    }

How should I process that JSON into those arrays?

Upvotes: 1

Views: 296

Answers (1)

Raghu K Nair
Raghu K Nair

Reputation: 3942

JSONArray.filters.getJSONArray() will return an JSONArray

Example :

JSONArray jArr = new JSONArray(result);
JSONArray j1 =  jArr.getJSONArray(0);   // 0th index. 

Here is the simplified answer for your code :

public class TestJSON {

    public static void main(String[] args) throws JSONException {
        String result = "[\n"
                + "  [\"53\",\"54\",\"55\",\"62\",\"63\",\"64\"],\n"
                + "  [\"1133 Budapest Dráva utca 10.\",\"1106 Budapest Örs vezér tere 25.\",\"1106 Budapest Örs vezér tere 25.\",\"1106 Budapest Örs vezér tere 25.\",\"1133 Budapest Dráva utca 10.\",\"1106 Budapest Örs vezér tere 25.\"],\n"
                + "  [\n"
                + "    [\"2016-08-18\",\"15:00:00\"],\n"
                + "    [\"2016-04-08\",\"13:00:00\"],\n"
                + "    [\"2016-04-08\",\"14:00:00\"],\n"
                + "    [\"2016-04-08\",\"10:00:00\"],\n"
                + "    [\"2016-04-08\",\"13:00:00\"],\n"
                + "    [\"2016-04-08\",\"15:00:00\"]\n"
                + "  ]\n"
                + "]";
        JSONArray jArr = new JSONArray(result);
        if (jArr.length() != 0) {
            List ids = fillArray(jArr.getJSONArray(0));
            List places = fillArray(jArr.getJSONArray(1));
            List dates = fillArray(jArr.getJSONArray(2));
            System.out.println(ids);
            System.out.println(places);
            System.out.println(dates);
        }
    }

    private static List fillArray(JSONArray jsonArray) throws JSONException {
        List array = new ArrayList();
        for (int i = 0; i < jsonArray.length(); i++) {
            array.add(jsonArray.get(i));
        }
        return array;
    }
}

Upvotes: 1

Related Questions