简体   繁体   中英

How do I consume an array of arrays using org.json?

how do i consume this json using org.json?

{
    "links": [
        [
            "a0b7a44b-333a-4ad8-aa1c-6689bb6265e5",
            {
                "label": "main",
                "currency": "GHS"
            }
        ],
        [
            "00000000-0000-0000-0000-000000000200",
            {
                "label": "main",
                "currency": "GHS"
            }
        ]
    ]
}

it appears to be an array of one-item arrays and i can't figure out how to unpack it.

Edit:

I see this question:

How to parse a json array with an array of arrays

that person is using gson, i'm using org.json. also the accepted answer to that question is:

Try using this:

 private List<List<Object>> f = new ArrayList<List<Object>>(); 

And then f.get(0).get(0) will give you a String of "type", and f.get(0).get(1) will give you a JsonObject

http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/JsonObject.html

  • i don't know how to get the data in to f
  • the link is broken

this did the trick.

        JSONObject obj = new JSONObject(inputString);

        JSONArray arr = obj.getJSONArray("links");
        for (int i = 0; i < arr.length(); i++) {
            System.out.println(arr.getString(i));
            JSONArray arr2 =arr.getJSONArray(i);
            for (int j = 0; j < arr2.length(); j++) {
                System.out.println(arr2.getString(j));
            }
            String s1 = arr2.getString(0);
            System.out.println(s1);
            JSONObject obj2 =arr2.getJSONObject(1);
            String s2 = obj2.getString("label");
            System.out.println(s2);
            String s3 = obj2.getString("currency");
            System.out.println(s3);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM