Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Gson在嵌套数组列表中给定null进行解析_Java_Android_Json_Parsing_Gson - Fatal编程技术网

Java 使用Gson在嵌套数组列表中给定null进行解析

Java 使用Gson在嵌套数组列表中给定null进行解析,java,android,json,parsing,gson,Java,Android,Json,Parsing,Gson,下面是我想要解析的json { "success": true, "status": 200, "events": [ { "event": { "artist_id": 54, "created_at": "2013-04-05T08:52:40Z", "duration": 2, "end_time": "2013-06-06T22:30:00Z",

下面是我想要解析的json

{

"success": true,
"status": 200,
"events": [
    {
        "event": {
            "artist_id": 54,
            "created_at": "2013-04-05T08:52:40Z",
            "duration": 2,
            "end_time": "2013-06-06T22:30:00Z",
            "event_desc": "Singer, composer, lyrist and Asia’s much lauded star, Jay Chou, will cast his spell on us once again on 6, 7 & 8 June at the Singapore Indoor Stadium at 8pm.\r\nHis most recent album “Opus 12” - the name symbolizing his 12th Mandarin album has been widely received and he now follows up with his eagerly anticipated world tour “OPUS JAY 2013 WORLD TOUR” which lets fans see Jay Chou’s\r\naccumulation of hard work since his first album in 2000.\r\nJay Chou rules with his trademark “Chou Style” known for his unusual brand of cross-genre pop, mixing Chinese and Western R & B beats sealing his status as the King of Mandopop.\r\n12 years on, Jay Chou still has plenty to give through his songs which cover a variety of genres and themes. Loyal fans can dance to Jay’s signature raps as well croon along with him though his soulful ballads.\r\nThis concert promises to be a spectacular experience from start to finish. Singapore is also Jay Chou’s third concert stop after Beijing and Shanghai and we get to experience this exciting and moving stage performance earlier in the tour!\r\nMark your calendars for a spectacular concert weekend and let the countdown begin!",
            "event_facebook_link": "http://www.facebook.com/events/490248764356139",
            "event_link": "http://www.sistic.com.sg/portal/dt?dt.isPortletRequest=true&dt.action=process&dt.provider=PortletWindowProcessChannel&dt.windowProvider.targetPortletChannel=JSPTabContainer/sEventsCalendar/Event&dt.containerName=JSPTabContainer/sEventsCalendar&dt.windowPr",
            "feature_small": false,
            "featured_status": true,
            "id": 51,
            "image": {
                "url": "/uploads/event/image/51/Event.jpg",
                "ratina_big": {
                    "url": "/uploads/event/image/51/ratina_big_Event.jpg"
                },
                "ratina_small": {
                    "url": "/uploads/event/image/51/ratina_small_Event.jpg"
                },
                "thumb_big": {
                    "url": "/uploads/event/image/51/thumb_big_Event.jpg"
                },
                "thumb_small": {
                    "url": "/uploads/event/image/51/thumb_small_Event.jpg"
                },
                "cell_big": {
                    "url": "/uploads/event/image/51/cell_big_Event.jpg"
                },
                "cell_small": {
                    "url": "/uploads/event/image/51/cell_small_Event.jpg"
                }
            }
下面是为解析定义的类

public class FeaturedPageData {

@SerializedName("status")
public String status;

@SerializedName("success")
public String success;

@SerializedName("events")
public ArrayList<Event> events = new ArrayList<FeaturedPageData.Event>();

public static class Event {

    public Event() {

    }

    @SerializedName("name")
    public String name;

    @SerializedName("end_time")
    public String end_time;

    public Images images;
}

public static class Images {

    public String ratina_big;
    public String ratina_small;
}

它显示了“success”和“status”的值,但arraylist显示name和end_time为null

我认为这是因为您实际上并没有解析一个事件对象数组,而是一个对象数组,其中包含一个名为“Event”的字段,该字段指向事件对象

"events": [
    {
        "event": {
您尚未为此容器对象声明类,因此无法对其进行分析


为外部对象创建一个类,或者修改json数组,使其直接包含事件对象。

正如在其他答案中已经指出的那样,问题是您试图将
“events”
json元素解析为
Event
的数组,因此Gson期望如下:

"events": [
    {
        "artist_id": 54,
        "created_at": "2013-04-05T08:52:40Z",
        "duration": 2,
        ...
    },
    ...
]
但实际上你有:

"events": [
    {
        "event": {
            "artist_id": 54,
            "created_at": "2013-04-05T08:52:40Z",
            "duration": 2,
            ...
        }
    },
    ...
]
请注意,有一个字符串
“event”
未包含在类模型中的任何位置


因此,您基本上有两种选择:

1。-您可以创建另一个类,比如说
EventItem
,只包含一个字段:

Event event;
并使用以下内容更改您的
FeaturedPageData
类:

ArrayList<EventItem> events;
通过这种方式,您告诉Gson数组
“events”
的内容是若干对:字符串(
“event”
)和一个对象
event
,这正是JSON响应中的内容


我认为第二种解决方案更好

下次您可以为我使用解决此类问题的最佳工具:)将ArrayList更改为List

谢谢Miko的回答。我尝试了两种方法,但即使我创建了另一类事件,它也会显示相同的错误。该事件显示预期的begin\u数组,但在第1行第41列是begin\u对象。对不起。我又犯了一个愚蠢的错误。你是冠军兄弟。你救了我一天。@MikO我想你发布的第二个JSON有一个小错误,所以我编辑了。这是你想要的代码吗?@everton是的,以前是错的,你的编辑很有意义,谢谢。
ArrayList<EventItem> events;
Map<String, Event> events;