Android Json 使用 GSON 解析

Android Json Parsing Using GSON

我在我的网络应用程序中使用 volley requests.I 我正在尝试使用 gson 库进行 json 解析并创建一个列表视图 that.Json 看起来像 this.When尝试迭代我只得到 null objects.What 是错误的,缺少什么

{
"header": {
    "count": 7,
    "expiryTime": 1000
},
"channel": [
    {
        "code": "/par/feed_section",
        "name": "News",
        "type": "news",
        "title": "News",
        "offline": "1",
        "sub": [
            {
                "parentCode": "/par/feed_section",
                "name": "Just In",
                "type": "news",
                "code": "/par/feed_sub_section",
                "title": "Just In",
                "offline": "1"
            },
            {
                "parentCode": "/par/feed_section",
                "name": "Kerala",
                "type": "news",
                "code": "/par/feed_sub_section_0",
                "title": "Kerala",
                "offline": "1"
            },
            {
                "parentCode": "/par/feed_section",
                "name": "Nation",
                "type": "news",
                "code": "/par/feed_sub_section_1",
                "title": "Nation",
                "offline": "1"
            },
            {
                "parentCode": "/par/feed_section",
                "name": "World",
                "type": "news",
                "code": "/par/feed_sub_section_2",
                "title": "World",
                "offline": "1"
            },
            {
                "parentCode": "/par/feed_section",
                "name": "Politics",
                "type": "news",
                "code": "/par/feed_sub_section_3",
                "title": "Politics",
                "offline": "1"
            },
            {
                "parentCode": "/par/feed_section",
                "name": "Diaspora",
                "type": "news",
                "code": "/par/feed_sub_section_5",
                "title": "Diaspora",
                "offline": "1"
            },
            {
                "parentCode": "/par/feed_section",
                "name": "Science & Tech",
                "type": "news",
                "code": "/par/feed_sub_section_6",
                "title": "Science & Tech",
                "offline": "1"
            },
            {
                "parentCode": "/par/feed_section",
                "name": "Opinion Pieces",
                "type": "news",
                "code": "/par/feed_sub_section_7",
                "title": "Opinion Pieces",
                "offline": "1"
            }
        ]
    },
    {
        "code": "/par/feed_section_5",
        "name": "Sports",
        "type": "news",
        "title": "Sports",
        "offline": "1",
        "sub": [
            {
                "parentCode": "/par/feed_section_5",
                "name": "Cricket",
                "type": "news",
                "code": "/par/feed_sub_section",
                "title": "Cricket",
                "offline": "1"
            },
            {
                "parentCode": "/par/feed_section_5",
                "name": "Football",
                "type": "news",
                "code": "/par/feed_sub_section_1",
                "title": "Football",
                "offline": "1"
            },
            {
                "parentCode": "/par/feed_section_5",
                "name": "Tennis",
                "type": "news",
                "code": "/par/feed_sub_section_2",
                "title": "Tennis",
                "offline": "1"
            },
            {
                "parentCode": "/par/feed_section_5",
                "name": "Motor Sports",
                "type": "news",
                "code": "/par/feed_sub_section_3",
                "title": "Motor Sports",
                "offline": "1"
            },
            {
                "parentCode": "/par/feed_section_5",
                "name": "Other Sports",
                "type": "news",
                "code": "/par/feed_sub_section_0",
                "title": "Other Sports",
                "offline": "1"
            }
        ]
    },
    {
        "code": "/par/feed_section_1",
        "name": "Business",
        "type": "news",
        "title": "Business",
        "offline": "1",
        "sub": [
            {
                "parentCode": "/par/feed_section_1",
                "name": "News",
                "type": "news",
                "code": "/par/feed_sub_section",
                "title": "News",
                "offline": "1"
            },
            {
                "parentCode": "/par/feed_section_1",
                "name": "Markets",
                "type": "news",
                "code": "/par/feed_sub_section_0",
                "title": "Markets",
                "offline": "1"
            },
            {
                "parentCode": "/par/feed_section_1",
                "name": "Companies",
                "type": "news",
                "code": "/par/feed_sub_section_1",
                "title": "Companies",
                "offline": "1"
            },
            {
                "parentCode": "/par/feed_section_1",
                "name": "Autos",
                "type": "news",
                "code": "/par/feed_sub_section_2",
                "title": "Autos",
                "offline": "1"
            }
        ]
    }
] } 

我的类就像

public class Channel {
  String code;
  String name;
  String type;
  String title;
  String offline;
  List<Sub> sub;  }

public class Sub {

String name;
String type;
String code;
String title;
String offline;
String parentCode; }

我使用截击并尝试像这样解析结果

GsonRequest<Channel> myReq = new GsonRequest<Channel>(Method.GET,
                                                "http://JSONURL/",
                                                Channel.class,
                                                createMyReqSuccessListener(),
                                                createMyReqErrorListener());

我被困在这个 point.How 来遍历 result.I 我是 GSON.So 的新手,请帮助我

您的域模型略有偏差,您需要有一个根对象并将通道设置为一个列表,因为它是您 json 中的一个数组。

试试这个:

import com.google.gson.Gson;

import java.util.List;


public class TestMe {

    public static void main(String[] args) {

        String jsonString = "paste your json here, omitted from example as its noise...";

        RootObject object = new Gson().fromJson(jsonString, RootObject.class);

        for (Channel c : object.channel){
            System.out.println(String.format("Channel %s has %d subs", c.name, c.sub.size()));
        }
    }
}
 class RootObject {
    Header header;
    List<Channel> channel;
}

 class Header {
    int count;
    int expirationTime;
}

 class Channel {
    String code;
    String name;
    String type;
    String title;
    String offline;
    List<Sub> sub;
}

 class Sub {

    String parentCode;
    String name;
    String type;
    String code;
    String title;
    String offline;
}

上面的示例输出:

Channel News has 8 subs
Channel Sports has 5 subs
Channel Business has 4 subs

希望对您有所帮助!