更新:
JsonObject:
pom.xml:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
bean ——》jsonString
A a = new A(3, "gg", "hh");
String s = JSONObject.toJSONString(a);
jsonString ——》bean
JSONObject jsonObject = JSONObject.parseObject(string);
A a = JSONObject.toJavaObject(jsonObject, A.class);
————————————————————————————————————————
1、jackson 框架:这个框架提供了JsonGenerator ,ObjectMapper两个类通过这两个类提供的方法可以将java 对象转化为json 对象,json 数组格式,也可以将json对象、数组格式转化为java对象。
2、json-lib框架也可以进行json格式和java 对象之间的相互转化,json-lib提供的类主要有:JSONObject ,JSONArray....
一、JSONObject,JSONArray
1、bean-》jason
public static void convertObject() {
Student stu=new Student("JSON","23","北京市西城区");
//1、使用JSONObject
JSONObject json = JSONObject.fromObject(stu);
//2、使用JSONArray
JSONArray array=JSONArray.fromObject(stu);
System.out.println(json.toString());
System.out.println(array.toString());
}
结果:
说明:JASONObject生成的是jason对象,JASONArray 生成的是jason数组。
2、jason-》javabean
public static void jsonStrToJava(){
//定义两种不同格式的字符串
String objectStr="{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城区\"}";
String arrayStr="[{\"name\":\"JSON\",\"age\":\"24\",\"address\":\"北京市西城区\"}]";
//1、使用JSONObject
JSONObject jsonObject=JSONObject.fromObject(objectStr);
Student stu=(Student)JSONObject.toBean(jsonObject, Student.class);
//2、使用JSONArray
JSONArray jsonArray=JSONArray.fromObject(arrayStr);
//获得jsonArray的第一个元素
JSONObject jsonObject2=JSONObject.fromObject(jsonArray.get(0));
Student stu2=(Student)JSONObject.toBean(jsonObject2, Student.class);
System.out.println("stu:"+stu);
System.out.println("stu2:"+stu2);
}
结果:
二、ObjectMapper
1、bean-》jason字符串
(1)bean
public void beanToJson() throws JsonProcessingException {
User user = new User(1, "xx", "666");
// 创建ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
System.out.println(json);
}
结果:
{"id":1,"name":"xx","password":"666"}
(2)list与map
public void beanToJson1() throws JsonProcessingException {
List<User> list = Arrays.asList(
new User(1, "gg", "888"),
new User(2, "hehe", "dada"),
new User(3, "lala", "papa"));
// 创建ObjectMapper
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(list);
System.out.println(json);
}
结果 :
[{"id":1,"name":"gg","password":"888"},{"id":2,"name":"hehe","password":"dada"},{"id":3,"name":"lala","password":"papa"}]
2、jason字符串-》bean
1、单个bean
public void method() throws IOException {
String str = "{\"id\":1,\"name\":\"aa\",\"password\":\"22\"}";
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(str, User.class);
System.out.println(user);
}
输出:
User{id=1, name='aa', password='22'}
2、转化为LIst或map
public void method2() throws IOException {
String str = "[{\"id\":1,\"name\":\"aa\",\"password\":\"22\"},{\"id\":2,\"name\":\"bb\",\"password\":\"66\"}]";
ObjectMapper objectMapper = new ObjectMapper();
List list = objectMapper.readValue(str, List.class);
System.out.println(list);
}
结果:
[{id=1, name=aa, password=22}, {id=2, name=bb, password=66}]