JSONUtil是针对JSONObject和JSONArray的静态快捷方法集合,在之前的章节我们已经介绍了一些工具方法,在本章节我们将做一些补充。
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"
}
/**
* 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
/**
* 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
/**
* 转换为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>
我们先定义两个较为复杂的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);
}
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦