android - 关于JSON格式转为JavaBean?
大家讲道理
大家讲道理 2017-04-18 09:43:18
0
4
437
[
    {
        "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解析就报这里错了。。。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

Antworte allen(4)
Peter_Zhu

这是一个JOSN数组转List的问题,比如对象如下

 public class TestBean{
     private String url;
     private String title;
     private String type;
     private String pic;
     //省略getter,setter
}

你给的json字符串转换成list应该是:
List<TestBean> list = gson.fromJson(str, new TypeToken<List<TestBean>>(){}.getType());

Ty80
public class Test{
    
    private List<TestBean> tests;
    //省略getter,setter
    
    public static class TestBean{
         private String url;
         private String title;
         private String type;
         private String pic;
         //省略getter,setter
    }
}

//这就能得到如题所示的json了
JSON.toJSON(Test.getTests())
Ty80

在json中,[]表示数组,{}表示对象,上面的结构,数组中有两个对象,每个对象有四个属性。数据结构为

List<Object> list = new ArrayList<Object>();
小葫芦

http://git.oschina.net/angryi...

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage