object toJSONObject,JSONArray,ObjectMapper分别实现json与bean
的转换
更新:
JsonObject:
<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 = JSONString(a);
jsonString ——》bean
JSONObject jsonObject = JSONObject.parseObject(string);
A a = JavaObject(jsonObject, A.class);
————————————————————————————————————————
1、jackson 框架:这个框架提供了JsonGenerator  ,ObjectMapper两个类通过这两个类提供的⽅法可以将java 对象转化为json 对象,json 数组格式,也可以将json对象、数组格式转化为java对象。
2、json-lib框架也可以进⾏json格式和java 对象之间的相互转化,json-lib提供的类主要有:JSONObject ,
⼀、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.String());
System.out.String());
}
结果:
说明: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=(Bean(jsonObject, Student.class);
//2、使⽤JSONArray
JSONArray jsonArray=JSONArray.fromObject(arrayStr);
//获得jsonArray的第⼀个元素
JSONObject jsonObject2=JSONObject.(0));
Student stu2=(Bean(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 = adValue(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 = adValue(str, List.class);
System.out.println(list);
}
结果:
[{id=1, name=aa, password=22}, {id=2, name=bb, password=66}]

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。