简体   繁体   English

使用GSON时发生IllegalStateException

[英]IllegalStateException when using GSON

im trying to read the following JSON file: 我试图读取以下JSON文件:

{ "rss" : {
     "@attributes" : {"version" : "2.0" },
      "channel" : { 
          "description" : "Channel Description",
          "image" : { 
              "link" : "imglink",
              "title" : "imgtitle",
              "url" : "imgurl"
            },

          "item" : {
              "dc_format" : "text",
              "dc_identifier" : "link",
              "dc_language" : "en-gb",
              "description" : "Description Here",
              "guid" : "link2",
              "link" : "link3",
              "pubDate" : "today",
              "title" : "Title Here"
            },

          "link" : "channel link",
          "title" : "channel title"
        }
    } 
}

Into this object: 进入这个对象:

public class RSSWrapper{
    public RSS rss;

    public class RSS{
        public Channel channel;
    }

    public class Channel{
        public List<Item> item;

    }
    public class Item{
        String description;//Main Content
        String dc_identifier;//Link
        String pubDate;
        String title;

    }
}

Im only interested in knowing what's in the "item" object so i assumed the above class would work when calling: 我只对知道“ item”对象中的内容感兴趣,因此我认为上述类在调用时会起作用:

Gson gson = new Gson();
RSSWrapper wrapper = gson.fromJson(JSON_STRING, RSSWrapper.class);

but im getting an error: 但我收到一个错误:

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT 线程“ main”中的异常com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY,但为BEGIN_OBJECT

I don't really know what this means so I don't know where to look for the error, hopefully someone with a better knowledge of GSON can help me? 我真的不知道这意味着什么,所以我不知道在哪里寻找错误,希望对GSON有更好了解的人可以帮助我?

Thanks :) 谢谢 :)

Your JSON string and RSSWrapper class are not compatible: the Channel expects to have a List<Item> while the JSON string contains one item. 您的JSON字符串和RSSWrapper类不兼容:当JSON字符串包含一项时, Channel希望具有List<Item> You have to either modify Channel as: 您必须将Channel修改为:

public class Channel{
    public Item item;

}

or the JSON as: 或JSON为:

"item" : [{
    "dc_format" : "text",
    "dc_identifier" : "link",
    "dc_language" : "en-gb",
    "description" : "Description Here",
    "guid" : "link2",
    "link" : "link3",
    "pubDate" : "today",
    "title" : "Title Here"
}],

to indicate that it is an array with one element. 表示它是一个有一个元素的数组。

If you control how the JSON input looks like, you're better off changing item to a JSON array 如果您控制JSON输入的外观,最好将item更改为JSON数组

"item" : [{
    "dc_format" : "text",
    "dc_identifier" : "link",
    "dc_language" : "en-gb",
    "description" : "Description Here",
    "guid" : "link2",
    "link" : "link3",
    "pubDate" : "today",
    "title" : "Title Here"
}]

If you do not and want your program to be able to handle both item array or object with the same RSSWrapper class; 如果您不这样做,并且希望程序能够使用相同的RSSWrapper类处理item数组或对象,那么请执行以下RSSWrapper here's a programmatic solution for you. 这是适合您的程序化解决方案。

JSONObject jsonRoot = new JSONObject(JSON_STRING);
JSONObject channel = jsonRoot.getJSONObject("rss").getJSONObject("channel");

System.out.println(channel);
if (channel.optJSONArray("item") == null) {
    channel.put("item", new JSONArray().put(channel.getJSONObject("item")));
    System.out.println(channel);
}

Gson gson = new Gson();
RSSWrapper wrapper = gson.fromJson(jsonRoot.toString(), RSSWrapper.class);

System.out.println(wrapper.rss.channel.item.get(0).title); // Title Here

Using the Java org.json parser, the code simply replaces the JSONObject by wrapping it into an array. 使用Java org.json解析器,代码只需将JSONObject包装到数组中即可替换它。 It leaves the JSON_STRING untouched if item is already a JSONArray . 如果item已经是JSONArrayJSON_STRING保持JSON_STRING不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gson 在尝试解析 List 时抛出 IllegalStateException - Gson throws IllegalStateException when attempting to parse List IllegalStateException-使用Gson使用String数组序列化地图 - IllegalStateException - Serializing a map with a String array using Gson 使用GSON将String解析为JsonObject会产生IllegalStateException:这不是JSON对象 - Parsing String to JsonObject using GSON gives IllegalStateException: This is not a JSON Object 使用GUI时出现错误:IllegalStateException - Errors when using GUI: IllegalStateException 使用Spring RequestContextHolder时发生IllegalStateException - IllegalStateException when using Spring RequestContextHolder 使用 ArrayList 和迭代器时出现非法状态异常 - illegalStateException when using ArrayList and iterator IllegalStateException:这不是JSON数组(Gson Java) - IllegalStateException: This is not a JSON Array (Gson Java) 使用GSON时类型不兼容 - Incompatible types when using GSON Gson IllegalStateException预期为BEGIN_OBJECT,但当数据类型为CharSequence时为STRING - Gson IllegalStateException Expected BEGIN_OBJECT but was STRING when data type is CharSequence java.lang.IllegalStateException:预期为 BEGIN_ARRAY,但在使用 Gson 库时为 STRING - java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING while using Gson library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM