首页 / 操作系统 / Linux / Java解析Json字符串--数组或列表
Java解析Json字符串--数组或列表Json示例:[
{
"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
}
]我们来解析一下这个Json字符串。首先,因为此Json字符串最外边是由一个中括弧”[]”包裹,那么,最终我们会用数组或者列表来接收它。接下来,我们能看到中括弧里边有三个同级的大括弧,并且每个大括弧里包含的内容是相同的,大括弧我们可以对应创建一个类,我们创建一个类Student.java,并对应大括弧内的元素,定义相应的成员变量,生成get/set方法。--------------------------------------分割线 --------------------------------------[译]JSON数据范式化(normalizr) http://www.linuxidc.com/Linux/2016-02/128288.htm如何处理JSON中的特殊字符 http://www.linuxidc.com/Linux/2015-09/123067.htmStruts中异步传送XML和JSON类型的数据 http://www.linuxidc.com/Linux/2013-08/88247.htmLinux下JSON库的编译及代码测试 http://www.linuxidc.com/Linux/2013-03/81607.htmjQuery 获取JSON数据[$.getJSON方法] http://www.linuxidc.com/Linux/2013-03/81673.htm用jQuery以及JSON包将表单数据转为JSON字符串 http://www.linuxidc.com/Linux/2013-01/77560.htm在C语言中解析JSON配置文件 http://www.linuxidc.com/Linux/2014-05/101822.htm--------------------------------------分割线 --------------------------------------我们生成的Student.java如下:package com.bean;/**
* 学生
*/
public class Student { private int age;//年龄
private String gender;//性别,male/female
private String grades;//班级
private String name;//姓名
private float weight;//体重 public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getGrades() {
return grades;
}
public void setGrades(String grades) {
this.grades = grades;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}}解析成数组或列表:package com.test;import java.util.ArrayList;
import java.util.List;import net.sf.json.JSONArray;import com.bean.Student;public class Domain { @SuppressWarnings("unchecked")
public static void main(String[] args) { 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}]"; JSONArray jsonArray = JSONArray.fromObject(jsonStr); Student[] stus = new Student[3];
List<Student> list = new ArrayList<Student>(); stus = (Student[]) JSONArray.toArray(jsonArray, Student.class);//转换成数组
list = (List<Student>) JSONArray.toCollection(jsonArray, Student.class);//转换成列表 }
}通过数组或者列表生成Json字符串:package com.test;import java.util.ArrayList;
import java.util.List;import net.sf.json.JSONArray;import com.bean.Student;public class Domain { @SuppressWarnings("unchecked")
public static void main(String[] args) { Student stu1 = new Student();
Student stu2 = new Student();
Student stu3 = new Student(); stu1.setAge(25);
stu1.setGender("female");
stu1.setGrades("三班");
stu1.setName("露西");
stu1.setWeight(51.3f); stu2.setAge(26);
stu2.setGender("male");
stu2.setGrades("三班");
stu2.setName("杰克");
stu2.setWeight(66.5f); stu3.setAge(25);
stu3.setGender("female");
stu3.setGrades("三班");
stu3.setName("莉莉");
stu3.setWeight(55.0f); Student[] stus = new Student[3];
List<Student> list = new ArrayList<Student>(); stus[0] = stu1;
stus[1] = stu2;
stus[2] = stu3; list.add(stu1);
list.add(stu2);
list.add(stu3); String jsonStr1 = JSONArray.fromObject(stus).toString();
String jsonStr2 = JSONArray.fromObject(list).toString(); System.out.println(jsonStr1);
System.out.println(jsonStr2);
}
}本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-02/128410.htm