json字符串转化为list泛型

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包如下:
json字符串转化为list泛型_第1张图片

项目源码下载地址(免积分):
http://download.csdn.net/download/zl544434558/8570563

你可能感兴趣的:(转化,json字符串,list泛型)