0

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.

0

5 Answers 5

1

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"));
Sign up to request clarification or add additional context in comments.

Comments

1

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");

Comments

0

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.

Comments

0

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

Comments

0
 //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")));

2 Comments

Could you add an explanation of the problem you code fixes from the original?
This is an example of how to do it with the full JSON as apposed to just the snippet. It also shows how to do it with BSON if you are working with MongoDB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.