Reading Json String using Gson "Non JSON Array" error results

In my project, I have a complex json answer. I want to read it using GSon.

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

It contains a JSONArray with the first "Item" and JSONObject with the second. Therefore, calling it leads to an 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.

+4
source share
3 answers

The string you are showing is a JSONObject not a JSONArray . Thus, in this case, you first need to get a JSONObject and do further decoding in that JSONObject.

 JSONObject - {} JSONArray - [] 

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

+5
source

Your JSON is valid, but not for 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 has key names starting with @. For JSON strings, this character is valid at the beginning of the name (see Right Column http://www.json.org/ ), but for Java these names are illegal (see Naming section http://download.oracle.com/javase/ tutorial / java / nutsandbolts / variables.html ). In particular, names starting with @ are annotations, and you cannot use annotation tags to declare variables, fields, methods, etc.

0
source

This is an invalid JSON object. Strings in JSON are always enclosed in double quotation marks ( " ). Contact the manufacturer of this JSON and ask him to use the correct encoder.

-1
source

All Articles