2

I have a below JSON Array and I am trying to parse it but it is giving me an exception:

[{
    "response": {
        "client": "123456",
        "111": {
            "data": "0\u00181535480381\u00191535480347\u0018\"voyager\";-1;12;0\u00181535480075\u00191535480069",
            "time": "981542121421"
        }
    }
}]

I am using org.json.JSONArray to parse the above JSON but below code throws exception:

String json =
    "[{ \"response\": { \"client\": \"123456\", \"111\": { \"data\": \"0\u00181535480381\u00191535480347\u0018\"voyager\";-1;12;0\u00181535480075\u00191535480069\", \"time\": \"981542121421\" } } }]";
 // this line throws exception
 JSONArray jsonArray = new JSONArray(json);

Here is the exception I am seeing:

Exception in thread "main" org.json.JSONException: Expected a ',' or '}' at character 81
    at org.json.JSONTokener.syntaxError(JSONTokener.java:410)
    at org.json.JSONObject.<init>(JSONObject.java:222)
    at org.json.JSONTokener.nextValue(JSONTokener.java:344)
    at org.json.JSONObject.<init>(JSONObject.java:205)
    at org.json.JSONTokener.nextValue(JSONTokener.java:344)
    at org.json.JSONObject.<init>(JSONObject.java:205)
    at org.json.JSONTokener.nextValue(JSONTokener.java:344)
    at org.json.JSONArray.<init>(JSONArray.java:125)
    at org.json.JSONArray.<init>(JSONArray.java:157)

What is wrong I am doing here?

12
  • You have some Unicode characters in the data field. My guess is that this has something to do with that. Commented Aug 29, 2018 at 1:56
  • I think this has to do with double quotes string voyager inside my json. Commented Aug 29, 2018 at 2:03
  • Those quotes are properly escaped though, with backslashes. You may test this by removing them and see what happens. Commented Aug 29, 2018 at 2:03
  • Yes once I remove double quotes around voyager then it works fine without any issues so something is weird. Commented Aug 29, 2018 at 2:04
  • Your raw text seems to parse fine for me (see here). I don't know what is wrong. Commented Aug 29, 2018 at 2:09

3 Answers 3

3

Put esacpe charaters around voyager like below.

\\\"voyager\\\"

I tested it worked.

import org.json.JSONArray;

public class Test {

    public static void main(String[] args) {
        String json = "[{ \"response\": { \"client\": \"123456\", \"111\": { \"data\": \"0\u00181535480381\u00191535480347\u0018\\\"voyager\\\";-1;12;0\u00181535480075\u00191535480069\", \"time\": \"981542121421\" } } }]";
        // this line throws exception
        JSONArray jsonArray = new JSONArray(json);
    }
}

Since it has already escape characters in JOSN you need to double escape in java to retain them.

Sign up to request clarification or add additional context in comments.

1 Comment

You also need to escape all \ characters, e.g. \u becomes \\u
2

\"voyager\"

This needs to be double escaped. The parser is seeing the \" as the end of the quote and expecting , or }

Try

\\\"voyager\\\"

Comments

1

In JSON syntax, you were wrong one place - "111", because names must be strings. thus, @NarayanP's code would not run on android system.

Your code throws exception, this is not json's mistake. problems are in assignment line;

String json = "...";

if you put below value into json through http response or file reading

"data": "0\u00181535480381\u00191535480347\u0018\"voyager\";-1;12;0\u00181535480075\u00191535480069"

then actually json's value will be

data: 015354803811535480347"voyager";-1;12;015354800751535480069  [escaped \u0018 etc. by stackoverflow]

if the JSON string contains a semicolon then only the part of the string up until the first semicolon encountered was being returned. thus, while parsing upper json string, data item will be same as

015354803811535480347"voyager"

Then "-1","12" are JSON syntax errors.

Following is full-code without errors.

String json = "[{\n" +
            "    \"response\": {\n" +
            "        \"client\": \"123456\",\n" +
            "        \"varname111\": {\n" +
            "            \"data\": \"0\\u00181535480381\\u00191535480347\\u0018\\\"voyager\\\";-1;12;0\\u00181535480075\\u00191535480069\",\n" +
            "            \"time\": \"981542121421\"\n" +
            "        }\n" +
            "    }\n" +
            "}]";

JSONArray jsonArray = null;

try {
    jsonArray= new JSONArray(json);
} catch (Exception e) {
    e.printStackTrace();
}

Comments

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.