piguy
piguy

Reputation: 526

Java parse JSON exception

I am trying to parse a JSON String using Java.

This is my code so far:

    JSONObject obj = new JSONObject(Connection.getDataWOProxy(proxyUseQ));
    JSONArray arr = obj.getJSONArray("result");
    for (int i = 0; i < arr.length(); i++) {
        System.out.println(arr.getInt(i));
    }

Info: Connection.getDataWOProxy(proxyUseQ); is a method I wrote that gets the JSON String from an URL using a proxy. proxyUseQ is a boolean that tells the method to use the proxy or not.

What should my code do normally:

JSONObject obj = new JSONObject(Connection.getDataWOProxy(proxyUseQ));

^ This line initializes my JSONObject giving it the String. Works great.

JSONArray arr = obj.getJSONArray("result");

^ Here I want it to access the "result" tree - works as well if I get it right. Now, if I understood it correctly, all my information should be saved in that Array, is this correct?

    for (int i = 0; i < arr.length(); i++) {
        System.out.println(arr.getInt(i));
    }

^ Now, I want to access the several information and print e.g. the short_description from result 2 (Console output should be: TEST 2). This is where my code fails:

org.json.JSONException: JSONArray[0] is not a number.

My JSON String:

{
  "result": [
    {
      "number": "1234",
      "short_description": "TEST",
      "priority": "4 - Low",
      "caller_id": "Some, User"
    },
    {
      "number": "12345",
      "short_description": "TEST 2",
      "priority": "4 - Low",
      "caller_id": "Some, User2"
    }
  ]
}

What am I doing wrong? I am new to JSON. Thanks.

Upvotes: 0

Views: 997

Answers (5)

Mateo
Mateo

Reputation: 2

 //import java.util.ArrayList;
 //import org.bson.Document;


 Document root = Document.parse("{ \"result\" : [{ \"number\" : \"1234\", \"short_description\" : \"TEST\", \"priority\" : \"4 - Low\", \"caller_id\" : \"Some, User\" }, { \"number\" : \"12345\", \"short_description\" : \"TEST 2\", \"priority\" : \"4 - Low\", \"caller_id\" : \"Some, User2\" }] }");

 System.out.println(((String)((Document)((ArrayList)root.get("result")).get(0)).get("number")));
 System.out.println(((String)((Document)((ArrayList)root.get("result")).get(0)).get("short_description")));
 System.out.println(((String)((Document)((ArrayList)root.get("result")).get(0)).get("priority")));
 System.out.println(((String)((Document)((ArrayList)root.get("result")).get(0)).get("caller_id")));
 System.out.println(((String)((Document)((ArrayList)root.get("result")).get(1)).get("number")));
 System.out.println(((String)((Document)((ArrayList)root.get("result")).get(1)).get("short_description")));
 System.out.println(((String)((Document)((ArrayList)root.get("result")).get(1)).get("priority")));
 System.out.println(((String)((Document)((ArrayList)root.get("result")).get(1)).get("caller_id")));

Upvotes: 0

Rajesh Gopu
Rajesh Gopu

Reputation: 893

arr.getInt(i) is wrong ,its first entry of "result" key of type JSONObject ,so getInt on JSONObject is invalid .

Below is the right procedure , to get only description

JSONArray arr = obj.getJSONArray("result");
for (int i = 0; i < arr.length(); i++) {
    JSONObject entry = arr.getJSONObject(i);
    System.out.println(entry.getString("short_description"));
}

ouput:

TEST
TEST 2

to print each key & values.

    JSONArray arr = obj.getJSONArray("result");
    for (int i = 0; i < arr.length(); i++) {
        JSONObject entry = arr.getJSONObject(i);
        Iterator<String> keyIter= entry.keys();
        System.out.println("---------------->Entry :" + (i+1));
        while(keyIter.hasNext()){
            String key = keyIter.next();
            System.out.print(key + " ==> ");
            System.out.print(entry.get(key)+"\n");
        }
    }

Output is

---------------->Entry :1
number ==> 1234
short_description ==> TEST
caller_id ==> Some, User
priority ==> 4 - Low
---------------->Entry :2
number ==> 12345
short_description ==> TEST 2
caller_id ==> Some, User2
priority ==> 4 - Low

Upvotes: 0

S. Stas
S. Stas

Reputation: 810

JSONArray[0] is not a number because it is an object. Based on your JSON String, you have array named "result", that contains two elements. arr.getInt(i) return the JSONArray[0] as JSONObject. You should call JSONObject obj = JSONArray.getJSONObject(i) and then you should call obj.getInt(i) Note, that your json fields are generally strings, not integers.

Upvotes: 0

Arpan Sharma
Arpan Sharma

Reputation: 2162

Yes because its not string or number its whole

{
      "number": "1234",
      "short_description": "TEST",
      "priority": "4 - Low",
      "caller_id": "Some, User"
    },

to get the number you have to

arr.getJSONObject(i).getString("number");

Upvotes: 1

shmosel
shmosel

Reputation: 50716

It's an array of JSON objects; they can't be converted to int. If you want to access a specific field on the current object, you can do so like this:

System.out.println(arr.getJSONObject(i).getString("short_description"));

Upvotes: 1

Related Questions