hutool工作总结


//判断是否为空
ObjectUtil.isNotNull
//判断集合是否为空
CollUtil.isNotEmpty()

 

hutool转换jsonobject
String jsonStr = "{\"b\":\"value2\",\"c\":\"value3\",\"a\":\"value1\"}";
//方法一:使用工具类转换
JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
//方法二:new的方式转换
JSONObject jsonObject2 = new JSONObject(jsonStr);
//对象转换成json字符串
JSONUtil.toJsonStr()

//如果我们想获得格式化后的JSON
JSONUtil.toJsonPrettyStr(sortedMap);
//JSON字符串解析
String html = "{\"name\":\"Something must have been changed since you leave\"}";
JSONObject jsonObject = JSONUtil.parseObj(html);
jsonObject.getStr("name");
//XML字符串转换为JSON
String s = "<sfzh>123</sfzh><sfz>456</sfz><name>aa</name><gender>1</gender>";
JSONObject json = JSONUtil.parseFromXml(s);

json.get("sfzh");
json.get("name");
Copy to clipboardErrorCopied
//JSON转换为XML
final JSONObject put = JSONUtil.createObj()
.set("aaa", "你好")
.set("键2", "test");

// <aaa>你好</aaa><键2>test</键2>
final String s = JSONUtil.toXmlStr(put);
Copy to clipboardErrorCopied
//hutool转list
List<GradeInterfaceItem> interfaceItem = JSONUtil.toList(jsonArray.getJSONObject(i).getJSONArray("interfaceItem"), GradeInterfaceItem.class);

//JSON转Bean
我们先定义两个较为复杂的Bean(包含泛型)

@Data
public class ADT {
private List<String> BookingCode;
}

@Data
public class Price {
private List<List<ADT>> ADT;
}
Copy to clipboardErrorCopied
String json = "{\"ADT\":[[{\"BookingCode\":[\"N\",\"N\"]}]]}";

Price price = JSONUtil.toBean(json, Price.class);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM