文章目录
前言
本站主要是记录下常用的api方法,完成常见的转换:
- Java对象(Bean、Map、List)转成JSON串
- Java对象(Bean、Map、List)转成JSON对象
- JSON串转成Java对象(Bean、Map、List)
- JSON串转成JSON对象
- JSON对象转成Java对象(Bean、Map、List)
- JSON对象转成JSON串
一、 Java对象(Bean、Map、List)—>JSON串
//JavaBean
People people = new People("老王",22);
String jsonStr = JSON.toJSONString(people);
System.out.println(jsonStr);
//Map
Map<String,Object> map = new HashMap<>();
map.put("name","老张");
map.put("age",16);
String jsonStr2 = JSON.toJSONString(map);
System.out.println(jsonStr2);
//List
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
String jsonStr3 = JSON.toJSONString(list);
System.out.println(jsonStr3);
二、Java对象(Bean、Map、List)—>JSON对象
//javaBean
JSONObject jsonObject = (JSONObject) JSON.toJSON(people);
String name = jsonObject.getString("name");
Integer age = jsonObject.getInteger("age");
System.out.println("name="+name+",age="+age);
//map
JSONObject jsonObject2 = (JSONObject) JSON.toJSON(map);
name = jsonObject2.getString("name");
age = jsonObject2.getInteger("age");
System.out.println("name="+name+",age="+age);
//list
JSONArray jsonArray = (JSONArray) JSON.toJSON(list);
jsonArray.forEach(item -> System.out.println(item));
三、JSON串—>Java对象(Bean、Map、List)
String json1 = "{\"name\":\"老张\",\"age\":16}";
String json2 = "[\"a\",\"b\",\"c\"]";
//javaBean
People people1 = JSON.parseObject(json1, People.class);
System.out.println("name="+people1.getName()+",age="+people1.getAge());
//map
Map map1 = JSON.parseObject(json1, Map.class);
System.out.println("name="+map1.get("name")+",age="+map1.get("age"));
//list
List list1 = JSON.parseObject(json2, List.class);
System.out.println(list1);
四、JSON串—>JSON对象
//JosnObject
JSONObject jsonObject1 = JSON.parseObject(json1);
name = jsonObject1.getString("name");
age = jsonObject1.getInteger("age");
System.out.println("name="+name+",age="+age);
//JsonArray
JSONArray jsonArray1 = JSON.parseArray(json2);
jsonArray1.forEach(item -> System.out.println(item));
五、JSON对象—>Java对象(Bean、Map、List)
//javaBean
People people2 = JSONObject.toJavaObject(jsonObject1, People.class);
System.out.println("name="+people2.getName()+",age="+people2.getAge());
Map map2 = JSONObject.toJavaObject(jsonObject1, Map.class);
System.out.println("name="+map2.get("name")+",age="+map2.get("age"));
List list2 = JSONArray.toJavaObject(jsonArray1, List.class);
System.out.println(list2);
六、JSON对象—>JSON串
String jsonString = JSONObject.toJSONString(jsonObject1);
System.out.println(jsonString);
String jsonString1 = JSONArray.toJSONString(jsonArray1);
System.out.println(jsonString1);
七、完整代码
public class TestFastJson {
public static void main(String[] args) {
People people = new People("老王",22);
String jsonStr = JSON.toJSONString(people);
System.out.println(jsonStr);
Map<String,Object> map = new HashMap<>();
map.put("name","老张");
map.put("age",16);
String jsonStr2 = JSON.toJSONString(map);
System.out.println(jsonStr2);
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
String jsonStr3 = JSON.toJSONString(list);
System.out.println(jsonStr3);
System.out.println("*******************************");
//javaBean
JSONObject jsonObject = (JSONObject) JSON.toJSON(people);
String name = jsonObject.getString("name");
Integer age = jsonObject.getInteger("age");
System.out.println("name="+name+",age="+age);
//map
JSONObject jsonObject2 = (JSONObject) JSON.toJSON(map);
name = jsonObject2.getString("name");
age = jsonObject2.getInteger("age");
System.out.println("name="+name+",age="+age);
//list
JSONArray jsonArray = (JSONArray) JSON.toJSON(list);
jsonArray.forEach(item -> System.out.println(item));
System.out.println("*******************************");
String json1 = "{\"name\":\"老张\",\"age\":16}";
String json2 = "[\"a\",\"b\",\"c\"]";
//javaBean
People people1 = JSON.parseObject(json1, People.class);
System.out.println("name="+people1.getName()+",age="+people1.getAge());
//map
Map map1 = JSON.parseObject(json1, Map.class);
System.out.println("name="+map1.get("name")+",age="+map1.get("age"));
//list
List list1 = JSON.parseObject(json2, List.class);
System.out.println(list1);
System.out.println("*******************************");
//JosnObject
JSONObject jsonObject1 = JSON.parseObject(json1);
name = jsonObject1.getString("name");
age = jsonObject1.getInteger("age");
System.out.println("name="+name+",age="+age);
//JsonArray
JSONArray jsonArray1 = JSON.parseArray(json2);
jsonArray1.forEach(item -> System.out.println(item));
System.out.println("*******************************");
//javaBean
People people2 = JSONObject.toJavaObject(jsonObject1, People.class);
System.out.println("name="+people2.getName()+",age="+people2.getAge());
Map map2 = JSONObject.toJavaObject(jsonObject1, Map.class);
System.out.println("name="+map2.get("name")+",age="+map2.get("age"));
List list2 = JSONArray.toJavaObject(jsonArray1, List.class);
System.out.println(list2);
System.out.println("*******************************");
String jsonString = JSONObject.toJSONString(jsonObject1);
System.out.println(jsonString);
String jsonString1 = JSONArray.toJSONString(jsonArray1);
System.out.println(jsonString1);
}
}
总结
本章主要内容:使用阿里的fastJson 完成 JSON对象、java对象、JSON串之间的相互转换
觉得不错的话,记得点击收藏,方便日后查找~