使用:
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.76</version> </dependency>
public class Demo3 {public static void main(String[] args) {Person person = new Person("zyq", 18);System.out.println("person:" + person);// 对象转换成json字符串String jsonString = JSON.toJSONString(person);System.out.println("jsonString:" + jsonString);// json字符串转换成对象Person person1 = JSON.parseObject(jsonString, Person.class);System.out.println("person1:" + person1);}
}@Data
@AllArgsConstructor
@NoArgsConstructor
class Person {private String name;private int age;
}
// 对象是集合List<Person> personList = new ArrayList<>();Person person = new Person("zyq", 18);Person person1 = new Person("zy", 19);personList.add(person);personList.add(person1);System.out.println("personList:" + personList);String jsonString = JSON.toJSONString(personList);System.out.println("jsonString:" + jsonString);