Accessing members of items in a JSONArray with Java

后端 未结 6 1562
小蘑菇
小蘑菇 2020-11-22 16:23

I\'m just getting started with using json with java. I\'m not sure how to access string values within a JSONArray. For instance, my json looks like this:

{
         


        
相关标签:
6条回答
  • 2020-11-22 16:46

    Have you tried using JSONArray.getJSONObject(int), and JSONArray.length() to create your for-loop:

    for (int i = 0; i < recs.length(); ++i) {
        JSONObject rec = recs.getJSONObject(i);
        int id = rec.getInt("id");
        String loc = rec.getString("loc");
        // ...
    }
    
    0 讨论(0)
  • 2020-11-22 16:48

    An org.json.JSONArray is not iterable.
    Here's how I process elements in a net.sf.json.JSONArray:

        JSONArray lineItems = jsonObject.getJSONArray("lineItems");
        for (Object o : lineItems) {
            JSONObject jsonLineItem = (JSONObject) o;
            String key = jsonLineItem.getString("key");
            String value = jsonLineItem.getString("value");
            ...
        }
    

    Works great... :)

    0 讨论(0)
  • 2020-11-22 16:50

    Java 8 is in the market after almost 2 decades, following is the way to iterate org.json.JSONArray with java8 Stream API.

    import org.json.JSONArray;
    import org.json.JSONObject;
    
    @Test
    public void access_org_JsonArray() {
        //Given: array
        JSONArray jsonArray = new JSONArray(Arrays.asList(new JSONObject(
                        new HashMap() {{
                            put("a", 100);
                            put("b", 200);
                        }}
                ),
                new JSONObject(
                        new HashMap() {{
                            put("a", 300);
                            put("b", 400);
                        }}
                )));
    
        //Then: convert to List<JSONObject>
        List<JSONObject> jsonItems = IntStream.range(0, jsonArray.length())
                .mapToObj(index -> (JSONObject) jsonArray.get(index))
                .collect(Collectors.toList());
    
        // you can access the array elements now
        jsonItems.forEach(arrayElement -> System.out.println(arrayElement.get("a")));
        // prints 100, 300
    }
    

    If the iteration is only one time, (no need to .collect)

        IntStream.range(0, jsonArray.length())
                .mapToObj(index -> (JSONObject) jsonArray.get(index))
                .forEach(item -> {
                   System.out.println(item);
                });
    
    0 讨论(0)
  • 2020-11-22 16:54

    By looking at your code, I sense you are using JSONLIB. If that was the case, look at the following snippet to convert json array to java array..

     JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( input );  
     JsonConfig jsonConfig = new JsonConfig();  
     jsonConfig.setArrayMode( JsonConfig.MODE_OBJECT_ARRAY );  
     jsonConfig.setRootClass( Integer.TYPE );  
     int[] output = (int[]) JSONSerializer.toJava( jsonArray, jsonConfig );  
    
    0 讨论(0)
  • 2020-11-22 17:00

    HashMap regs = (HashMap) parser.parse(stringjson);

    (String)((HashMap)regs.get("firstlevelkey")).get("secondlevelkey");

    0 讨论(0)
  • 2020-11-22 17:01

    In case it helps someone else, I was able to convert to an array by doing something like this,

    JSONObject jsonObject = (JSONObject)new JSONParser().parse(jsonString);
    ((JSONArray) jsonObject).toArray()
    

    ...or you should be able to get the length

    ((JSONArray) myJsonArray).toArray().length
    
    0 讨论(0)
提交回复
热议问题