package com.phone.week6.day5.fastjson;
public class Person {
private String name;
private int age;
private String address;
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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Person(String name, int age, String address) {
super();
this.name = name;
this.age = age;
this.address = address;
}
public Person() {
super();
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", address=" + address
+ "]";
}
}
package com.phone.week6.day5.fastjson;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
public class Test {
public static void main(String[] args) {
test2();
}
private static void test2() {
Person p = new Person("线三", 80, "SZ");
String str=JSON.toJSONString(p);
System.out.println(str);
List<Person> list = new ArrayList<>();
list.add(new Person("a", 20, "sb"));
list.add(new Person("b", 20, "sa"));
list.add(new Person("c", 20, "sc"));
str = JSON.toJSONString(list);
System.out.println(str);
}
private static void test1() {
String str = "{name:'king',age:20,address:'sz'}";
Person p = JSON.parseObject(str, Person.class);
System.out.println(p);
String arrayJson="[{'name':'张三1','age':25},{'name':'张三2','age':25},{'name':'张三3','age':25}]";
List<Person> list=JSON.parseArray(arrayJson, Person.class);
for (Person person : list) {
System.out.println(person);
}
}
}