Java解析Json字符串--数组或列表

Json示例:

 1 [
 2 {
 3 "age": 25,
 4 "gender": "female",
 5 "grades": "三班",
 6 "name": "露西",
 7 "weight": 51.3
 8 },
 9 {
10 "age": 26,
11 "gender": "male",
12 "grades": "三班",
13 "name": "杰克",
14 "weight": 66.5
15 },
16 {
17 "age": 25,
18 "gender": "female",
19 "grades": "三班",
20 "name": "莉莉",
21 "weight": 55
22 }
23 ]

 

我们来解析一下这个Json字符串。

首先,因为此Json字符串最外边是由一个中括弧”[]”包裹,那么,最终我们会用数组或者列表来接收它。

接下来,我们能看到中括弧里边有三个同级的大括弧,并且每个大括弧里包含的内容是相同的,大括弧我们可以对应创建一个类,我们创建一个类Student.java,并对应大括弧内的元素,定义相应的成员变量,生成get/set方法。

我们生成的Student.java如下:

 1 package com.bean;
 2 
 3 /**
 4 * 学生
 5 */
 6 public class Student {
 7 
 8 private int age;//年龄
 9 private String gender;//性别,male/female
10 private String grades;//班级
11 private String name;//姓名
12 private float weight;//体重
13 
14 public int getAge() {
15 return age;
16 }
17 public void setAge(int age) {
18 this.age = age;
19 }
20 public String getGender() {
21 return gender;
22 }
23 public void setGender(String gender) {
24 this.gender = gender;
25 }
26 public String getGrades() {
27 return grades;
28 }
29 public void setGrades(String grades) {
30 this.grades = grades;
31 }
32 public String getName() {
33 return name;
34 }
35 public void setName(String name) {
36 this.name = name;
37 }
38 public float getWeight() {
39 return weight;
40 }
41 public void setWeight(float weight) {
42 this.weight = weight;
43 }
44 
45 }

 

解析成数组或列表:

 1 package com.test;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import net.sf.json.JSONArray;
 7 
 8 import com.bean.Student;
 9 
10 public class Domain {
11 
12 @SuppressWarnings("unchecked")
13 public static void main(String[] args) {
14 
15 String jsonStr = "[{\"age\": 25,\"gender\": \"female\",\"grades\": \"三班\",\"name\": \"露西\",\"weight\": 51.3},{\"age\": 26,\"gender\": \"male\",\"grades\": \"三班\",\"name\": \"杰克\",\"weight\": 66.5},{\"age\": 25,\"gender\": \"female\",\"grades\": \"三班\",\"name\": \"莉莉\",\"weight\": 55}]";
16 
17 JSONArray jsonArray = JSONArray.fromObject(jsonStr);
18 
19 Student[] stus = new Student[3];
20 List<Student> list = new ArrayList<Student>();
21 
22 stus = (Student[]) JSONArray.toArray(jsonArray, Student.class);//转换成数组
23 list = (List<Student>) JSONArray.toCollection(jsonArray, Student.class);//转换成列表
24 
25 }
26 }

 

通过数组或者列表生成Json字符串:

 1 package com.test;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import net.sf.json.JSONArray;
 7 
 8 import com.bean.Student;
 9 
10 public class Domain {
11 
12 @SuppressWarnings("unchecked")
13 public static void main(String[] args) {
14 
15 Student stu1 = new Student();
16 Student stu2 = new Student();
17 Student stu3 = new Student();
18 
19 stu1.setAge(25);
20 stu1.setGender("female");
21 stu1.setGrades("三班");
22 stu1.setName("露西");
23 stu1.setWeight(51.3f);
24 
25 stu2.setAge(26);
26 stu2.setGender("male");
27 stu2.setGrades("三班");
28 stu2.setName("杰克");
29 stu2.setWeight(66.5f);
30 
31 stu3.setAge(25);
32 stu3.setGender("female");
33 stu3.setGrades("三班");
34 stu3.setName("莉莉");
35 stu3.setWeight(55.0f);
36 
37 Student[] stus = new Student[3];
38 List<Student> list = new ArrayList<Student>();
39 
40 stus[0] = stu1;
41 stus[1] = stu2;
42 stus[2] = stu3;
43 
44 list.add(stu1);
45 list.add(stu2);
46 list.add(stu3);
47 
48 String jsonStr1 = JSONArray.fromObject(stus).toString();
49 String jsonStr2 = JSONArray.fromObject(list).toString();
50 
51 System.out.println(jsonStr1);
52 System.out.println(jsonStr2);
53 
54 
55 }
56 }
上一篇:MySQL数据库之子查询


下一篇:20用d编程结构