I have a JSON text as response from a web API:
{
"code": 0,
"message": "Success",
"data": {
"invoiceId": "26825",
"additionService": [
2
],
"invoiceStatusTxt":"OK"
}
}
I'm using JSONParser to get a JSONArray from the additionService key. But, it is showing a JSON error. My code is:
JSONObject RES = JSONParser.getJSON(URL_API);
JSONArray ob = RES.getJSONObject("data").getJSONArray("additionService");
The error messages are as follows:
W/System.err﹕ org.json.JSONException: No value for additionService
W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:355)
W/System.err﹕ at org.json.JSONObject.getJSONObject(JSONObject.java:574)
i has used json parser before, when i get value from key invoiceId.. it work, i have problem with additionService –
I tried it this way. Seems to be working:
String str = "{\"code\":0,\"message\":\"Success\",\"data\":{\"invoiceId\":\"26825\",\"additionService\":[2],\"invoiceStatusTxt\":\"OK\"}}";
JSONObject obj=(JSONObject) JSONValue.parse(str);
JSONObject data = (JSONObject) obj.get("data");
JSONArray additionService = (JSONArray) data.get("additionService");
System.out.println(additionService);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With