maven依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.15</version>
</dependency>
HutoolTest.java
package com.xjz;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zhwy.pojo.SurfChnMulMin;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;
public class HutoolTest {
//JSON字符串创建
@Test
public void toJsonStr() {
SortedMap<Object, Object> sortedMap = new TreeMap<Object, Object>() {
private static final long serialVersionUID = 1L;
{
put("attributes", "a");
put("b", "b");
put("c", "c");
}
};
System.out.println(JSONUtil.toJsonStr(sortedMap));
System.out.println(JSONUtil.toJsonPrettyStr(sortedMap));
}
//Json字符串解析
@Test
public void parseObj() {
String html = "{\"name\":\"Something must have been changed since you leave\"}";
JSONObject jsonObject = JSONUtil.parseObj(html);
System.out.println(jsonObject.getStr("name"));
}
//XML字符串转换成JSON
@Test
public void parseFormXml() {
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"));
System.out.println(json.get("name"));
}
//Json转换为XML
@Test
public void toXmlStr() {
final JSONObject put = JSONUtil.createObj()
.set("aaa", "您好")
.set("xjz", "666");
System.out.println(JSONUtil.toXmlStr(put));
}
//JSONObject代表一个JSON中的键值对象,这个对象以大括号包围,每个键值对使用,隔开,键与值使用:隔开,一个JSONObject类似于这样:
// {
// "key1":"value1",
// "key2":"value2"
// }
//创建
@Test
public void createObj() {
JSONObject json1 = JSONUtil.createObj()
.put("key", "value1")
.put("key2", "value2");
JSONObject json2 = new JSONObject()
.set("key1", "v1")
.set("key2", "v2");
System.out.println(json1);
System.out.println(json2);
}
//1. Json字符串解析
@Test
public void toStringPretty() {
String jsonStr = "{\"b\":\"value2\",\"c\":\"value3\",\"a\":\"value1\"}";
//方法一:使用工具类转换
JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
//方法二:new的方式转换
JSONObject jsonObject2 = new JSONObject(jsonStr);
//JSON对象转字符串(一行)
System.out.println(jsonObject2);
// 也可以美化一下,即显示出带缩进的JSON:
System.out.println(jsonObject2.toStringPretty());
}
//JavaBean解析
@Test
public void jsonUtilParseObj() {
SurfChnMulMin scmm = new SurfChnMulMin();
scmm.setStationIdC("A00001");
scmm.setStationName("xjz_2002");
// false 表示不跳过null值
JSONObject json = JSONUtil.parseObj(scmm, false);
System.out.println(json);
System.out.println(json.toJSONString(5));
System.out.println(json.toStringPretty());
}
//在JSON中,JSONArray代表一个数组,使用中括号包围,每个元素使用逗号隔开。一个JSONArray类似于这样:
//["value1","value2","value3"]
//创建
@Test
public void createJsonArray() {
JSONArray array = JSONUtil.createArray();
JSONArray array2 = new JSONArray();
array.add("value1");
array.add("value2");
array.add("value3");
//转为JsonArray字符串
System.out.println(array);
}
//从Bean列表解析
@Test
public void beanListParse() {
SurfChnMulMin scmm = new SurfChnMulMin();
scmm.setStationIdC("A00001");
scmm.setStationName("xjz_2002");
SurfChnMulMin scmm2 = new SurfChnMulMin();
scmm2.setStationIdC("A00001");
scmm2.setStationName("xjz_2002");
ArrayList<SurfChnMulMin> list = CollUtil.newArrayList(scmm, scmm2);
// JSONArray jsonArray = JSONUtil.parseArray(list);
JSONArray jsonArray = new JSONArray(list);
System.out.println(jsonArray.getJSONObject(0).getStr("stationName"));
}
//从JSON字符串解析
@Test
public void toParseArray() {
String jsonStr = "[\"value1\", \"value2\", \"value3\"]";
JSONArray array = JSONUtil.parseArray(jsonStr);
System.out.println("array=" + array);//array=["value1","value2","value3"]
}
//转换为bean的List
@Test
public void toList() {
String jsonArr = "[{\"stationIdC\":111,\"stationName\":\"test1\"},{\"stationIdC\":112,\"stationName\":\"test2\"}]";
JSONArray array = JSONUtil.parseArray(jsonArr);
System.out.println("array=" + array);
//array=[{"id":111,"name":"test1"},{"id":112,"name":"test2"}]
List<SurfChnMulMin> userList = JSONUtil.toList(array, SurfChnMulMin.class);
// 111
System.out.println(userList.get(0).getStationIdC());
}
//转换为Dict的List
@Test
public void parseDictList() {
String jsonArr = "[{\"id\":111,\"name\":\"test1\"},{\"id\":112,\"name\":\"test2\"}]";
JSONArray array = JSONUtil.parseArray(jsonArr);
List<Dict> list = JSONUtil.toList(array, Dict.class);
// 111
System.out.println(list.get(0).getInt("id"));
}
//转换为数组
@Test
public void parseArray() {
String jsonArr = "[{\"stationIdC\":111,\"stationName\":\"test1\"},{\"stationIdC\":112,\"stationName\":\"test2\"}]";
JSONArray array = JSONUtil.parseArray(jsonArr);
SurfChnMulMin[] list = array.toArray(new SurfChnMulMin[0]);
}
//Json路径
@Test
public void getByPath() {
String jsonStr = "[{\"id\": \"1\",\"name\": \"a\"},{\"id\": \"2\",\"name\": \"b\"}]";
final JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
// b
System.out.println(jsonArray.getByPath("[1].name"));
}
}
HuTool 中文文档:https://doc.hutool.cn/pages/json/