Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JSON.Simple with nested objects and arrays

Tags:

java

json

I've decided on using JSON.Simple to parse Java in my application, as apposed to GSON or Jackson, because both of them seem over complicated for my needs and appeared to need additional class files to work as intended. I have the following JSON:

{
    "request":{
        "act":"rec_load_all",
        "email":"Redacted",
        "tkn":"Redacted",
        "a":"rec_load_all",
        "z":"Redacted"
    },
    "response":{
        "recs":{
            "has_more":false,
            "count":9,
            "objs":[{
                "rec_id":"1385442465",
                "rec_hash":"1825780e334bcd831034bd9ca62",
                "zone_name":"Redacted",
                "name":"Redacted",
                "display_name":"Redacted",
                "type":"A",
                "prio":null,
                "content":"Redacted",
                "display_content":"Redacted",
                "ttl":"1",
                "ttl_ceil":86400,
                "ssl_id":null,
                "ssl_status":null,
                "ssl_expires_on":null,
                "auto_ttl":1,
                "service_mode":"1",
                "props":{
                    "proxiable":1,
                    "cloud_on":1,
                    "cf_open":0,
                    "ssl":0,
                    "expired_ssl":0,
                    "expiring_ssl":0,
                    "pending_ssl":0,
                    "vanity_lock":0
                }
            }]
        }
    },
    "result":"success",
    "msg":null
}

The objs array lists 9 different items, but I only included one for simplicity. I need to get has_more, count, and the id within objs. I've tried:

JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(responseString);
JSONArray objs = (JSONArray) jsonObject.get("objs");
Iterator<JSONObject> iterator = objs.iterator();
while (iterator.hasNext()) {
    JSONObject idObj = (JSONObject) iterator.next();
    String id = (String) idObj.get("rec_id");
    System.out.println(id);
}

But it fires a java.lang.NullPointerException error, so I'm assuming because it's nested under response -> recs -> objs it isn't getting a value. I'm also following a few year old tutorial, so something could have changed since. If you could explain whats wrong and an example of how to fix it, I would greatly appreciate it, I learn by seeing.

EDIT: Full error

Exception in thread "main" java.lang.NullPointerException
    at ddns.Net.getId(Net.java:46)
    at ddns.DDNS.main(DDNS.java:7)
Java Result: 1

ddns.Net.getId(Net.java:46) | Iterator<JSONObject> iterator = objs.iterator(); ddns.DDNS.main(DDNS.java:7) | Just calls the method

like image 834
Novicode Avatar asked Jul 19 '14 15:07

Novicode


1 Answers

You have to take any json object from parent and so on, take a look this code (I tried and works):

    public static void main(String[] args) throws IOException, ParseException {

    JSONParser jsonParser = new JSONParser();

    InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("json.json");
    InputStreamReader reader = new InputStreamReader(is);
    JSONObject result = (JSONObject) jsonParser.parse(reader);
    JSONObject response = (JSONObject) result.get("response");
    JSONObject recs = (JSONObject) response.get("recs");

    boolean hasMore = (Boolean) recs.get("has_more");
    JSONArray objs = (JSONArray) recs.get("objs");
    System.out.println(recs);
    System.out.println("has more " + hasMore);

    Iterator objIter = objs.iterator();
    int i = 0;
    while (objIter.hasNext()) {
        i++;
        System.out.println(String.format("obj %d: %s", i, objIter.next()));
    }

}

There is another way that for me is easier and is to create a Java Bean with the same structure so... you will parse a hole object, if you are interested on this approach let me know.

like image 155
Carlos Verdes Avatar answered Oct 19 '22 17:10

Carlos Verdes