最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Reading Json String using Gson results error "not a JSON Array" - Stack Overflow

programmeradmin5浏览0评论

In my project i have a plex 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<.servlet.action.ItemInfo>  and  java.lang.IllegalStateException: This is not a JSON Array.

Please help how i should handle this scenario. Thanks.

In my project i have a plex 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<.servlet.action.ItemInfo>  and  java.lang.IllegalStateException: This is not a JSON Array.

Please help how i should handle this scenario. Thanks.

Share Improve this question asked Sep 7, 2011 at 8:30 NeeteshNeetesh 9171 gold badge6 silver badges17 bronze badges 3
  • could you post some parsing code?, looks like you are trying to read jsonArray on jsonObject – Yashwanth Kumar Commented Sep 7, 2011 at 8:32
  • Valid JSON uses " instead of '. That might not be your problem, but just in case. – Py. 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? – Thomas Commented Sep 7, 2011 at 8:34
Add a ment  | 

3 Answers 3

Reset to default 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(")

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./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/) but for Java this names are illegal (see Naming section http://download.oracle./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.

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.

发布评论

评论列表(0)

  1. 暂无评论