文章预览:
- JSONUtil
- 介绍
- 使用
-
- 1、JSON字符串创建
- 2、JSON字符串解析
- 3、XML字符串转换为JSON
- 4、JSON转换为XML
- 5、 JSON转Bean
JSONUtil
介绍
JSONUtil是针对JSONObject和JSONArray的静态快捷方法集合,在之前的章节我们已经介绍了一些工具方法,在本章节我们将做一些补充。
使用
1、JSON字符串创建
JSONUtil.toJsonStr可以将任意对象(Bean、Map、集合等)直接转换为JSON字符串。 如果对象是有序的Map等对象,则转换后的JSON字符串也是有序的。
/**
* 转换为JSON字符串
* <p>
* 被转为JSON的对象
*
* @return JSON字符串
*/
@Test
void toJsonStrTest() {
//map
SortedMap<Object, Object> sortedMap = new TreeMap<Object, Object>() {
private static final long serialVersionUID = 1L;
{
put("attributes", "a");
put("b", "b");
put("c", "c");
}
};
//对象
Student beanString = new Student(1, 1, 1, "张三");
//集合
List<Student> list = new ArrayList<>();
list.add(new Student(1, 1, 1, "张三"));
list.add(new Student(1, 1, 2, "李四"));
list.add(new Student(1, 1, 3, "王五"));
System.out.println("JSONUtil.toJsonStr(beanString) = " + JSONUtil.toJsonStr(beanString));
System.out.println("JSONUtil.toJsonStr(sortedMap) = " + JSONUtil.toJsonStr(sortedMap));
System.out.println("JSONUtil.toJsonStr(list) = " + JSONUtil.toJsonStr(list));
}
结果
JSONUtil.toJsonStr(beanString) = {"gradeId":0,"studentId":1,"termId":1,"classId":1,"name":"张三"}
JSONUtil.toJsonStr(sortedMap) = {"attributes":"a","b":"b","c":"c"}
JSONUtil.toJsonStr(list) = [{"gradeId":0,"studentId":1,"termId":1,"classId":1,"name":"张三"},
{"gradeId":0,"studentId":2,"termId":1,"classId":1,"name":"李四"},
{"gradeId":0,"studentId":3,"termId":1,"classId":1,"name":"王五"}]
如果我们想获得格式化后的JSON,则:
JSONUtil.toJsonPrettyStr(object);
System.out.println("JSONUtil.toJsonPrettyStr(sortedMap) = "
+ JSONUtil.toJsonPrettyStr(sortedMap));
结果
JSONUtil.toJsonPrettyStr(sortedMap) = {
"attributes": "a",
"b": "b",
"c": "c"
}
2、JSON字符串解析
/**
* JSON字符串转JSONObject对象
* <p>
* JSON字符串
*
* @return JSONObject
*/
@Test
void Test() {
String html = "{\"name\":\"Something must have been changed since you leave\"}";
JSONObject jsonObject = JSONUtil.parseObj(html);
System.out.println("jsonObject.getStr(\"name\") = " + jsonObject.getStr("name"));
}
结果
jsonObject.getStr("name") = Something must have been changed since you leave
3、XML字符串转换为JSON
/**
* XML字符串转为JSONObject
*
* XML字符串
* @return JSONObject
*/
@Test
void parseFromXmlTest() {
String s = "<sfzh>123</sfzh><sfz>456</sfz><name>aa</name><gender>1</gender>";
JSONObject json = JSONUtil.parseFromXml(s);
System.out.println("json.get(\"sfzh\") = " + json.get("sfzh"));
System.out.println("json.get(\"name\") = " + json.get("name"));
}
结果
json.get("sfzh") = 123
json.get("name") = aa
4、JSON转换为XML
/**
* 转换为XML字符串
*
* @param json JSON
* @return XML字符串
*/
@Test
void toXmlStrTest() {
final JSONObject put = JSONUtil.createObj()
.set("aaa", "你好")
.set("键2", "test");
// <aaa>你好</aaa><键2>test</键2>
final String s = JSONUtil.toXmlStr(put);
System.out.println("s = " + s);
}
结果
s = <aaa>你好</aaa><键2>test</键2>
5、 JSON转Bean
我们先定义两个较为复杂的Bean(包含泛型)
@Data
public class ADT {
private List<String> BookingCode;
}
@Data
public class Price {
private List<List<ADT>> ADT;
}
/**
* JSON字符串转为实体类对象,转换异常将被抛出
*
* @param <T> Bean类型
* @param jsonString JSON字符串
* @param beanClass 实体类对象
* @return 实体类对象
* @since 3.1.2
*/
@Test
void toBeanTest() {
String json = "{\"ADT\":[[{\"BookingCode\":[\"N\",\"N\"]}]]}";
Price price = JSONUtil.toBean(json, Price.class);
System.out.println("price = " + price);
}
相关文章
暂无评论...