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