[
{
"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解析就报这里错了。。。
天蓬老师2017-04-18 09:45:18
This is a problem of converting JOSN array to List. For example, the object is as follows
public class TestBean{
private String url;
private String title;
private String type;
private String pic;
//省略getter,setter
}
The json string you gave you converted into a list should be:
List<TestBean> list = gson.fromJson(str, new TypeToken<List<TestBean>>(){}.getType());
PHP中文网2017-04-18 09:45:18
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())
PHP中文网2017-04-18 09:45:18
In json, [] represents an array and {} represents an object. In the above structure, there are two objects in the array, and each object has four properties. The data structure is
List<Object> list = new ArrayList<Object>();