我是用的阿里的这个版本包
<!-- JSON 处理工具 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>
json对象到json字符串之间的转换
Student student = new Student("小王", 200);
String s = JSON.toJSONString(student);
打印 语句如下:
{"name":"小王","old":200}
json字符串到json对象之间的转换
Student student = new Student("小王", 200);
String s = JSON.toJSONString(student);
Student student1 = JSONObject.parseObject(s,Student.class);
打印语句如下:
Student(name=小王, old=200)
集合类性的字符串和集合的转变
List<Student> list = new ArrayList(20);
for (int i = 0; i < 20; i++) {
list.add(
new Student("小王", 200)
);
}
//集合转换为json字符串
String j = JSON.toJSONString(list);
//集合类型的json字符串,转换为集合
List<Student> list1 = JSONArray.parseArray(j,Student.class);
打印语句如下:
[Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200), Student(name=小王, old=200)]