猿问

关于JSON格式转为JavaBean?

[

    {

        "url": "http://www.baidu.com",

        "title": "测试1",

        "type": 1,

        "pic": "null"

    },

    {

        "url": "http://www.baidu.com",

        "title": "测试2",

        "type": 2,

        "pic": "null"

    }

]

例如如上的JSON格式,其对应的JavaBean是什么样子的?

[]括号括起来的应该是对应List,但是其List并没有一个key,这该怎么弄?

Gson解析就报这里错了。。。


富国沪深
浏览 434回答 3
3回答

慕丝7291255

这是一个JOSN数组转List的问题,比如对象如下&nbsp;public class TestBean{&nbsp; &nbsp; &nbsp;private String url;&nbsp; &nbsp; &nbsp;private String title;&nbsp; &nbsp; &nbsp;private String type;&nbsp; &nbsp; &nbsp;private String pic;&nbsp; &nbsp; &nbsp;//省略getter,setter}你给的json字符串转换成list应该是:List<TestBean> list = gson.fromJson(str, new TypeToken<List<TestBean>>(){}.getType());

潇潇雨雨

public class Test{&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; private List<TestBean> tests;&nbsp; &nbsp; //省略getter,setter&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public static class TestBean{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;private String url;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;private String title;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;private String type;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;private String pic;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//省略getter,setter&nbsp; &nbsp; }}//这就能得到如题所示的json了JSON.toJSON(Test.getTests())

MMTTMM

在json中,[]表示数组,{}表示对象,上面的结构,数组中有两个对象,每个对象有四个属性。数据结构为List<Object>&nbsp;list&nbsp;=&nbsp;new&nbsp;ArrayList<Object>();
随时随地看视频慕课网APP

相关分类

Java
我要回答