json字符串格式如下:
String jsonStr = "{\"data\":[{\"id\":\"001\",\"name\":\"evan\",\"age\":\"18\"},null,{\"id\":\"002\",\"name\":\"zl\",\"age\":\"20\"}]}";
具体解析源码如下:
public class JsonConvertDemo {
public static void main(String[] args) {
String jsonStr = "{\"data\":[{\"id\":\"001\",\"name\":\"evan\",\"age\":\"18\"},null,{\"id\":\"002\",\"name\":\"zl\",\"age\":\"20\"}]}";
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
String jsonArr = jsonObject.get("data").toString();
JSONArray jsonArray = JSONArray.fromObject(jsonArr);
List<User> userList = new ArrayList<User>();
for (int i = 0; i < jsonArray.size(); i++) {
User user = (User) JSONObject.toBean( JSONObject.fromObject(jsonArray.get(i)), User.class);
userList.add(user);
}
}
}
还需要一个User实体类,源码如下:
public class User {
private String id;
private String name;
private String age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String toString (){
return "id:"+id+"---------->"+"name:"+name+"---------->"+"age:"+age;
}
}
需要的jar包如下:
项目源码下载地址(免积分):
http://download.csdn.net/download/zl544434558/8570563
时间: 2024-10-06 22:48:59