Java parse JSON exception

nodejs json parse error
unhandled exception type jsonexception
org.json.simple.parser.parseexception maven
json in java
generic json parser java
json parse invalid character
exception handling in json
import org json-simple parser parseexception

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.

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

Java Code Examples org.json.simple.parser.ParseException, ParseException. The following are top voted examples for showing how to use org.json.simple.parser.ParseException. These examples are extracted from  I am trying to parse a JSON string in java to have the individual value printed separately. But while making the program run I get the following error- Exception in thread "main" java.lang.

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

JsonParsingException (Java(TM) EE 7 Specification APIs), public class JsonParsingException extends JsonException. JsonParsingException is used when an incorrect JSON is being parsed. Author: Jitendra Kotamraju  The following are top voted examples for showing how to use org.json.simple.parser.ParseException.These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.

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.

JSONException, Exception · java.io.IOException · com.fasterxml.jackson.core.​JsonProcessingException public class JsonParseException extends JsonProcessingException. Java ParseException Example In this example, we use DateFormat.parse (String source) method which throws ParseException object. This parse () method throws ParseException - if the beginning of the specified string cannot be parsed. Here is a complete code to throw ParseException exception:

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

JsonParseException (Jackson-core 2.2.0 API), This page provides Java code examples for org.json.simple.parser.​ParseException. @throws ParseException Throw this exception if any */ public static Map  How to parse JSON in Java JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data exchange format that is easy for humans and machines to read and write. JSON can represent two structured types: objects and arrays.

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

Java Code Examples of org.json.simple.parser.ParseException, The best way to catch invalid JSON parsing errors is to put the calls to JSON.​parse() to a try/catch block.Examplefunction parseJSONSafely(str)  On decoding, the default concrete class of java.util.List is org.json.simple.JSONArray and the default concrete class of java.util.Map is org.json.simple.JSONObject. Encoding JSON in Java. Following is a simple example to encode a JSON object using Java JSONObject which is a subclass of java.util.HashMap. No ordering is provided.

JsonParseException, catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString​()); Constructs a ParseException with the specified detail message and offset. A detail message is a String that describes this particular exception.

Proper way to catch exception from JSON.parse, java.lang.RuntimeException. groovy.json.JsonException. All Implemented whenever a problem occurs when creating or parsing JSON data structures. This is the exception thrown. com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY The exception is clear that its expecting an object (probably the Datapoint), but it encountered an array.

org.json.JSONException.toString java code examples, The processing of JavaScript Object Notation (JSON) in Java is done through Java application programming interface using JavaScript Object Notation, i.e., JSON.simple, The JavaScript Object Notation JSON.simple is a library in java which allows parsing JavaScript Object Notation, generating JavaScript Object Notation, transforming JavaScript Object Notation, querying JavaScript Object Notation etc.

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.