要求:解析下面JSON数据
String string = “{no:1,name:’Android’,employees:[{name:’zhangsan’,age:20},{name:’lisi’,age:21},{name:’wangwu’,age:22}]}”;
代码:
package com.qf.demo4;
import java.util.ArrayList;
import com.alibaba.fastjson.JSON;
public class Test {
public static void main(String[] args) {
//fastjson解析数据
//1.阿里解析过程和Gson类似,
String string= "{no:1,name:'android',employees:[{name:'zhangsan',age:20},{name:'lisi',age:21},{name:'wangwu',age:22}]}";
Employ employ = JSON.parseObject(string,Employ.class);
System.out.println(employ);
}
}
class Person{
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
class Employ{
private int no;
private String name;
private ArrayList<Person> employees;
public Employ(int no, String name, ArrayList<Person> employees) {
super();
this.no = no;
this.name = name;
this.employees = employees;
}
public Employ() {
super();
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<Person> getEmployees() {
return employees;
}
public void setEmployees(ArrayList<Person> employees) {
this.employees = employees;
}
@Override
public String toString() {
return "Employ [no=" + no + ", name=" + name + ", employees=" + employees + "]";
}
}