3

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 –

bulubuloa
  • 597
  • 1
  • 10
  • 21
  • 1
    Please also post the error that you are getting – Bernd Linde Apr 14 '15 at 11:37
  • i has updated my ans – bulubuloa Apr 14 '15 at 11:42
  • Its showing that there is no value for the key 'additionService' in the json object you get from RES.getJSONObject("data"). Can you print whats in 'RES.getJSONObject("data")' and comment that – Anita Apr 14 '15 at 11:47
  • 1
    For the above code I am getting value for `additionService` are you sure the JSON is correct ? – Sachin Gupta Apr 14 '15 at 11:49
  • 2
    @SachinGupta I agree, there is no error in presented JSON, are you sure that printing URL_API will generate above Json String? – Beri Apr 14 '15 at 11:53
  • When you're first learning JSON you should avoid "long chain polymer" statements like the plague. Do it one step at a time and dump what you received after each step. – Hot Licks Apr 14 '15 at 12:03
  • i has used jsonparser before, when i get value from key invoiceId.. it work, i have problem with additionService – bulubuloa Apr 14 '15 at 12:10

4 Answers4

1

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);
LittlePanda
  • 2,496
  • 1
  • 21
  • 33
0

The exception is thrown at the getJSONObject("data") call, so the data field is empty. You should make an #isNull() check on the value returned by #getJsonObject(). Then, if it exists, get the array from it.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
0

Your input data is corrent. I assume that your problem lies somewhere else, either your input String is wrong, or your library.

Little test that works (JDK7):

  @Test
  public void testJson() {
    String URL_API = "{\"code\": 0,\"message\": \"Success\",\"data\": {\"invoiceId\": \"26825\",\"additionService\": [2],\"invoiceStatusTxt\":\"OK\"}}";

    JSONObject RES = new JSONObject(URL_API);
    JSONArray ob = RES.getJSONObject("data").getJSONArray("additionService");
    System.out.print(ob); // prints [2]
  }

My pom.xml dependency:

  <dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20140107</version>
 </dependency>
Beri
  • 11,470
  • 4
  • 35
  • 57
0

You can also use JSONParser parse method

JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(URL_API);
JSONObject dataObject= (JSONObject) jsonObject.get("data");
JSONArray additionService = (JSONArray) dataObject.get("additionService");

In your case I think there is type cast error.

Kanti
  • 1,062
  • 1
  • 10
  • 27