1

I am trying to get a list of objects into an array, it works fine when the objects are all good, but when i get 4000 records its hard to see which one is malformed. How can i check which part of the string is malformed or skip that specific object.. it would be better if i can pin point where it is getting malformed but like i said, with 4000 records the error does not help me much. Here is my code:

JsonParser parser = new JsonParser();
JsonReader jreader = new JsonReader(new StringReader(result));
jreader.setLenient(true);

JsonElement elem = parser.parse(reader); //throws malformed json error
JsonArray contacts = elem.getAsJsonArray();

Gson converter = new Gson();
ContactObject obj = null;

Type cons = new TypeToken<ArrayList<Contact>>(){}.getType();
temp = converter.fromJson(contacts, cons);

Here is the error:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 38670
    at com.google.gson.internal.Streams.parse(Streams.java:56)
    at com.google.gson.JsonParser.parse(JsonParser.java:84)
    at com.android.companya.ContactServer$APICalls.doInBackground(APIServer.java:737)
    at com.android.companya.ContactServer$APICalls.doInBackground(APIServer.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:856)
 Caused by: com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 38670
    at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1310)
    at com.google.gson.stream.JsonReader.nextInObject(JsonReader.java:722)
    at com.google.gson.stream.JsonReader.peek(JsonReader.java:382)
    at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:349)
    at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:657)
    at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:650)
    at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:633)
    at com.google.gson.internal.Streams.parse(Streams.java:44)
1
  • Why not just work with JSONObject or JSONArray are loop through it yourself? Commented Aug 20, 2013 at 17:04

2 Answers 2

3

Its for sure we get this error when json coming from server is invalid.

Using try catch wont help you much whats wrong in your server code so, in order to catch the exact issue you just need to follow following steps:

1) I copied all the json and validated it in online json validator tool

for example:

  1. http://jsonlint.com/
  2. http://json.parser.online.fr/

both tools are very efficient and pin point you the exact error if you are server side developer yourself then you can fix it easily otherwise tell your poor developer to fix it.

hope this is the detailed answer.

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

Comments

1

Simply use try/catch construct. That's what it's meant for.

JsonParser parser = new JsonParser();
JsonReader jreader = new JsonReader(new StringReader(result));
jreader.setLenient(true);

try {
    JsonElement elem = parser.parse(reader); //throws malformed json error
    JsonArray contacts = elem.getAsJsonArray();

    Gson converter = new Gson();
    ContactObject obj = null;

    Type cons = new TypeToken<ArrayList<Contact>>(){}.getType();
    temp = converter.fromJson(contacts, cons);
} catch(JsonSyntaxException ex) {
    // Inform then user that the the Json data contains invalid syntax
}

Also you can't skip a malformed object. Either the whole result is valid or it's not. You will have to fix this on your server who creates the Json string. It's important to correctly escape the values. In i.e. PHP you have json_encode(...) method with parameters for escaping (PHP 5.3 or newer) it's content.

5 Comments

So there is no way to pin-point what is causing the malformed object? - issue is i cant manipulate the server code, i am getting this from external API and i doubt they will change it.
Well, you can pin-point it. The Exception tells where the error happens: Unterminated object at line 1 column 38670. Basically the whole JSON is one single line and the error happens in the 38671th character. Depending on the error it also may be an error in the JSON library you are using.
I don't know which JSON library you are using, but I had good experience with google-gson @code.google.com/p/google-gson If you update your question with the JSON object that appears at the point of exception (see comment above) then people may tell you if it's rather to be an bug from your library or if the server is creating invalid JSON results.
I am using gson, the problem is that the exception is not showing me the JSON as you can see in the above exception.
It is showing the position. And you have your whole JSON data in a string. JsonReader jreader = new JsonReader(new StringReader(result)); The result variable contains the string with the whole JSON. You just have to print it out (or save as a textFile) and look at position 38670 in the text editor of your choice.

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.