共计 4204 个字符,预计需要花费 11 分钟才能阅读完成。
Json 是一种轻量级的数据交换格局, 其语法格局非常简单
对象:{key : value, key: value…}
数组:[元素 1, 元素 2 …]
在平时的开发中,咱们都须要应用 Json 在前后端之间进行数据交换,这里咱们次要来看一下 Java 最方便快捷的解析 Json 的形式
- 谷歌 Gson
将对象转为 JsonString toJson(Object src)
将 json 转为对象
<T> T fromJson(String json, Class<T> classOfT)
将 json 转为汇合
<T> T fromJson(String json, Type typeOfT)
代码示例:
/**
* 将对象转换为 json
* @param o
* @return
*/
public static String getJsonString(Object o){return new Gson().toJson(o);
}
/**
* 将 json 转为对象
* @param jsonString
* @param clazz
* @return
*/
public static Object getObjectFromJson(String jsonString, Class<?> clazz){return new Gson().fromJson(jsonString, clazz);
}
/**
* 将 json 转为汇合
* @param jsonString
* @param type
* @return
*/
public static Object getObjectFromJson(String jsonString, Type type){return new Gson().fromJson(jsonString, type);
}
public static void main(String[] args) {
// 对象转 json
String objectJson = getJsonString(new Student(18, "jason"));
// 后果:{"age":18,"name":"jason"}
System.out.println(objectJson);
// json 转对象
Student student = (Student) getObjectFromJson("{\"age\":18,\"name\":\"jason\"}", Student.class);
// 后果:Student{age=18, name='jason'}
System.out.println(student);
// List
List<Student> list = new ArrayList<>();
list.add(new Student(18, "jason"));
list.add(new Student(19, "lee"));
// 汇合转 json
String listJson = getJsonString(list);
// 后果:[{"age":18,"name":"jason"}, {"age":19,"name":"lee"}]
System.out.println(listJson);
// Json 转 List
Type type = new TypeToken<ArrayList<Student>>() {}.getType();
ArrayList<Student> arrayList = (ArrayList<Student>) getObjectFromJson(listJson, type);
// 后果:[Student{age=18, name='jason'}, Student{age=19, name='lee'}]
System.out.println(arrayList.toString());
// HashMap
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("name", "jason");
hashMap.put("age", "18");
// HashMap 转 json
String mapJson = getJsonString(hashMap);
// 后果:{"name":"jason","age":"18"}
System.out.println(mapJson);
// json 转 HashMap
Type mapType = new TypeToken<HashMap<String, String>>() {}.getType();
HashMap<String, String> map = (HashMap<String, String>) getObjectFromJson(mapJson, mapType);
/**
* 后果
* key:name, value:jason
* key:age, value:18
*/
for (Map.Entry<String, String> entry : map.entrySet()){System.out.println("key:" + entry.getKey() + ", value:" + entry.getValue());
}
}
- 阿里 FastJson
对象转 jsonstatic String toJSONString(Object object)
json 转对象
static <T> T parseObject(String text, Class<T> clazz)
json 转 List
static <T> List<T> parseArray(String text, Class<T> clazz)
json 转 Map
static <T> T parseObject(String text, TypeReference<T> type, Feature... features)
代码示例:
/**
* 对象转 json
* @param o
* @return
*/
public static String getJsonString(Object o){return JSON.toJSONString(o);
}
/**
* json 转对象
* @param jsonString
* @param clazz
* @return
*/
public static Object getObjectFromJson(String jsonString, Class<?> clazz){return JSON.parseObject(jsonString, clazz);
}
/**
* json 转 list
* @param jsonString
* @param clazz
* @return
*/
public static Object getArrayFromJson(String jsonString, Class<?> clazz){return JSON.parseArray(jsonString, clazz);
}
/**
* json 转 map
* @param jsonString
* @param typeReference
* @return
*/
public static Object getMapFromJson(String jsonString, TypeReference<?> typeReference){return JSON.parseObject(jsonString, typeReference);
}
public static void main(String[] args) {
// 对象转 json
String objectJson = getJsonString(new Student(18, "jason"));
// 后果:{"age":18,"name":"jason"}
System.out.println(objectJson);
// json 转对象
Student student = (Student) getObjectFromJson(objectJson, Student.class);
// 后果:Student{age=18, name='jason'}
System.out.println(student);
// List
List<Student> list = new ArrayList<>();
list.add(new Student(18, "jason"));
list.add(new Student(19, "lee"));
// List 转 json
String listJson = getJsonString(list);
// 后果:[{"age":18,"name":"jason"},{"age":19,"name":"lee"}]
System.out.println(listJson);
// json 转 List
List<Student> students = (List<Student>) getArrayFromJson(listJson, Student.class);
// 后果:[Student{age=18, name='jason'}, Student{age=19, name='lee'}]
System.out.println(students.toString());
// Map
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("name", "jason");
hashMap.put("age", "18");
// Map 转 json
String mapJson = getJsonString(hashMap);
// 后果:{"name":"jason","age":"18"}
System.out.println(mapJson);
// json 转 HashMap
TypeReference<HashMap<String, String>> typeReference = new TypeReference<HashMap<String, String>>() {};
HashMap<String, String> map = (HashMap<String, String>) getMapFromJson(mapJson, typeReference);
/**
* 后果
* key:name, value:jason
* key:age, value:18
*/
for (Map.Entry<String, String> entry : map.entrySet()){System.out.println("key:" + entry.getKey() + ", value:" + entry.getValue());
}
}
正文完