解析JSON字符串

{
    "data": {
             "json1": [
                        {
                                "time": "05-17",
                                "msg": 0
                              },
                              {
                                "time": "05-18",
                                "msg": 2
                              }
                            ],
                            "json2": [
                              {
                                "time": "05-17",
                                "msg": 0
                              },
                              {
                                "time": "05-18",
                                "msg": 2
                              }
                            ]

                          }
                        }


public class data {

private   Map> data;

public Map> getData() {
    return data;
}

public void setData(Map> data) {
    this.data = data;
}


}

public class M {
    private String time;
    private int msg;
    /**
     * @return the time
     */
    public String getTime() {
        return time;
    }
    /**
     * @param time the time to set
     */
    public void setTime(String time) {
        this.time = time;
    }
    /**
     * @return the msg
     */
    public int getMsg() {
        return msg;
    }
    /**
     * @param msg the msg to set
     */
    public void setMsg(int msg) {
        this.msg = msg;
    }

}


public static void main(String[] args) {
        // TODO Auto-generated method stub
String str="{\"data\": {\"json1\": [{\"time\": \"05-17\",\"msg\": 0},{\"time\": \"05-18\",\"msg\": 1"
                  +"}],\"json2\": [ {\"time\": \"05-17\",\"msg\": 2},{\"time\": \"05-18\",\"msg\": 3}]}}";

            JSONObject jsonObject=JSONObject.fromObject(str);
            JSONObject jso=jsonObject.getJSONObject("data");

            data d=(data)JSONObject.toBean(jsonObject,data.class);
            Map> list=d.getData();

            for(String m:list.keySet()){
                List ml=list.get(m);
                JSONArray arr=JSONArray.fromObject(ml);
                for(Object ob:arr){
                    JSONObject joj=JSONObject.fromObject(ob);
                    M j=(M)JSONObject.toBean(joj,M.class); 
                    System.out.println(j.getMsg()+","+j.getTime());
                }
                }
            }
{
    "name": "三班",
    "students": [
        {
            "age": 25,
            "gender": "female",
            "grades": "三班",
            "name": "露西",
            "score": {
                "网络协议": 98,
                "JavaEE": 92,
                "计算机基础": 93
            },
            "weight": 51.3
        },
        {
            "age": 26,
            "gender": "male",
            "grades": "三班",
            "name": "杰克",
            "score": {
                "网络安全": 75,
                "Linux操作系统": 81,
                "计算机基础": 92
            },
            "weight": 66.5
        },
        {
            "age": 25,
            "gender": "female",
            "grades": "三班",
            "name": "莉莉",
            "score": {
                "网络安全": 95,
                "Linux操作系统": 98,
                "SQL数据库": 88,
                "数据结构": 89
            },
            "weight": 55
        }
    ]
}

1.

    private String name;
    private List students;
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the students
     */
    public List getStudents() {
        return students;
    }
    /**
     * @param students the students to set
     */
    public void setStudents(List students) {
        this.students = students;
    }

2.
    private int age;
    private String gender;
    private String grades;
    private String name;
    private float weight;
    private Map score;
    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }
    /**
     * @return the gender
     */
    public String getGender() {
        return gender;
    }
    /**
     * @param gender the gender to set
     */
    public void setGender(String gender) {
        this.gender = gender;
    }
    /**
     * @return the grades
     */
    public String getGrades() {
        return grades;
    }
    /**
     * @param grades the grades to set
     */
    public void setGrades(String grades) {
        this.grades = grades;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the weight
     */
    public float getWeight() {
        return weight;
    }
    /**
     * @param weight the weight to set
     */
    public void setWeight(float weight) {
        this.weight = weight;
    }
    /**
     * @return the score
     */
    public Map getScore() {
        return score;
    }
    /**
     * @param score the score to set
     */
    public void setScore(Map score) {
        this.score = score;
    }
    3.
        String jsonstr="{\"name\":\"三班\",\"students\":[{\"age\":25,\"gender\":\"female\","
                + "\"grades\":\"三班\",\"name\":\"露西\",\"score\":{\"网络协议\":98,\"JavaEE\":92,\"计算机基础\""
                + ":93},\"weight\":51.3},{\"age\":25,\"gender\":\"female\",\"grades\":\"三班\",\"name\":\"丽丽\""
                + ",\"score\":{\"网络安全\":95,\"操作\":98,\"数据库\":88,\"数据结构\":89},\"weight\":55}]}";

        JSONObject jo=JSONObject.fromObject(jsonstr);

        Xinxi xx=(Xinxi)JSONObject.toBean(jo,Xinxi.class);


        int strstrat=jsonstr.indexOf("[");
        int endstrat=jsonstr.lastIndexOf("]")+1;
            //将数据分成jsonArray
            String jsonStr=jsonstr.substring(strstrat, endstrat);
            System.out.println("jsonStr:"+jsonStr);
            JSONArray jsonArray=new JSONArray();
            jsonArray =JSONArray.fromObject(jsonStr);
            System.out.println("jsonArray:"+jsonArray);
            for (Object object : jsonArray) {
                JSONObject jsonObject=JSONObject.fromObject(object);

                Ren r=(Ren) JSONObject.toBean(jsonObject, Ren.class); 
                System.out.println("r:"+r.getScore());
            }

你可能感兴趣的:(json解析,json)