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.