4

In my project i have a complex json response. I want to read it by GSon.

JSON : {'FoodMenuRS':{'Results':[{'Items':{'Item':[{'@Id':'24'},{'@Id':'24'}]}}, {'Items':{'Item':{'@Id':'24'}}}]}}

It contains a JSONArray with first "Item" and JSONObject with second one. Hence its call results in error,

failed to deserialize json object {"@Id":"24"} given the type java.util.List<com.servlet.action.ItemInfo>  and  java.lang.IllegalStateException: This is not a JSON Array.

Please help how i should handle this scenario. Thanks.

3
  • could you post some parsing code?, looks like you are trying to read jsonArray on jsonObject Commented Sep 7, 2011 at 8:32
  • Valid JSON uses " instead of '. That might not be your problem, but just in case. Commented Sep 7, 2011 at 8:33
  • {"@Id":"24"} is indeed not a JSon array. Dare to share some Java code here, esp. the classes you want to deserialize that to? Commented Sep 7, 2011 at 8:34

3 Answers 3

6

The string you are showing is a JSONObject not a JSONArray. So, in this case you first of all have to get the JSONObject and perform further decoding on that JSONObject.

JSONObject - {}

JSONArray - []

And indeed JSONObject or JSONArray should be encoded using Double-quotes(")

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

Comments

0

Your JSON is valid, but not for the doble quotes (") because JSON supports simple quotes (') and no quotes in the key name. See http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Colle

However this JSON have key names that begin with @. For JSON strings this character is valid at the beginning of the name (see right column http://www.json.org/) but for Java this names are illegal (see Naming section http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html). Specifically, names started with @ are annotations and you can't use annotations tags to declare variables, fields, methods, etc.

Comments

-1

This is not a valid JSON object. Strings in JSON are always encapsulated in double quotes ("). Contact the producer of that JSON and tell him to use a correct encoder.

1 Comment

Theorically is not a valid JSON string but Gson supports " and ' and no quotes, like many other parsers. Also Javascript supports simple quotes and no quotes :) See sites.google.com/site/gson/…

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.