3

I have a need to parse a JSON string containing Objects, but there can also be Arrays in the JSON, which I don't need, and it's currently crashing with:

com.google.gson.JsonSyntaxException: 
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY

If I remove all the Arrays from the JSON, it works perfectly fine to parse the JSON with my POJO using the following code:

Type type = new TypeToken<Map<String, UsersPOJO>>(){}.getType();
Map<String, UsersPOJO> myUsers = gson.fromJson(JSONString, type);

But I'm having no luck parsing whenever there's Arrays in the JSON. I don't need, nor do I want to parse the Arrays, but if it's necessary, parsing the Arrays and then discarding the result would be okay.

How do I accomplish this with Gson? Or any other Java JSON library for that matter. Gson isn't a requirment.

This is an example of the JSON I'd be parsing:

{
   "1002001":{  
      "level":2,
      "name":"CaptKrunch",
      "uid":1002001,
      "user":{  
         "age":21,
         "city":"None",
         "country":"United States",
         "creation":1269969663
      },
      "meta":{  
         "score":1762,
         "rank":78
      }
   },
   "1003001":{  
      "level":11,
      "name":"LtRaine",
      "uid":1003001,
      "user":{  
         "age":35,
         "city":"LA",
         "country":"United States",
         "creation":1269369663
      },
      "meta":{  
         "score":11562,
         "rank":11
      }
   },
   "tags_1002001": [
     "conqurer",
     "almighty"
   ]
}
4
  • The problem here is that your JSON data is not uniform. Which is something that should be fixed on the side of the data provider, not in the parsing code. Assuming you could preprocess the JSON you'd have to remove the top-level elements that contain unexpected data (aka. the arrays) Commented Apr 8, 2016 at 23:18
  • @Vogel612 The generation of the JSON data is not something I can control. I've been tasked with parsing it for a client app and can not change anything on the server side I'm afraid. This would be no problem to parse if I could use C# but alas, Java is required. Can't this be done in Java with the currently available Json libraries? Commented Apr 8, 2016 at 23:20
  • Here which all you want to removed or ignored ??? Commented Apr 9, 2016 at 22:51
  • @SachinDivakar I want to ignore all JSONArrays (in the above example "tags_1002001", but there could be hundreds, if not thousands). Commented Apr 9, 2016 at 23:01

1 Answer 1

1
+50

You can skip array, if parse JSON string to JsonElement and iterate all elements:

Gson gson = new Gson();

//Type type = new TypeToken<Map<String, UsersPOJO>>(){}.getType();
//Map<String, UsersPOJO> myUsers = gson.fromJson(jsonString, type);

JsonParser parser = new JsonParser();
JsonElement topElement = parser.parse(jsonString);
Map<String, UsersPOJO> myUsers = new HashMap<>();
for (Map.Entry<String, JsonElement> entry : topElement.getAsJsonObject().entrySet()) {
    if (entry.getValue().isJsonArray()) {
        //skip or process array
    } else {
        myUsers.put(entry.getKey(), gson.fromJson(entry.getValue(), UsersPOJO.class));
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

that's exactly what I was looking for! I was in the processing of doing something similar with entry.getValue/getKey, but reconstructing a new JSON string! :-) But this solution is exactly what I was looking for!
bounty will be awarded in 15 hours, that's the earliest SO will let me! :-) Thanks!

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.