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
idObjobject might be null. It would be strange forgetto throw NPE. It might return null but I wouldn't expect it to throw NPE.