fastjson 将json和java对象相互转换
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
package com.suning.sdipwd.controller.fourthPage;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSON;
public class MyTest {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException {
Person p1 = new Person();
p1.setAge(1);
p1.setName("zhangsan");
String s = JSON.toJSONString(p1);
System.out.println(s);
String jsonPerson = "{\"age\":1,\"name\":\"zhangsan\"}";
Person p2 = JSON.parseObject(jsonPerson, Person.class);
p2.setAge(2);
p2.setName("lisi");
System.out.println(p2);
List<Person> list = new ArrayList<>();
list.add(p1);
list.add(p2);
String listJson = JSON.toJSONString(list);
System.out.println(listJson);
String listJson1 = "[{\"age\":1,\"name\":\"zhangsan\"},{\"age\":2,\"name\":\"lisi\"}]";
List<Person> list1 = JSON.parseArray(listJson1, Person.class);
System.out.println(list1);
}
}
更多推荐
所有评论(0)