Accessing members of items in a JSONArray with Java

后端 未结 6 1560
小蘑菇
小蘑菇 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: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... :)

提交回复
热议问题