JSON常用的工具方法
前言:
在日常开发中,JSON 数据的处理是常见的需求。无论是数据转换、格式化还是与其他格式的互转,掌握一些常用的工具方法可以大大提高开发效率。本文将介绍一些实用的 JSON 操作方法,帮助你快速上手。
JSON常用的工具方法
- 1.json字符串转换成list
- 2.json字符串转换成对象
- 3.json转换xml
- 4.xml转换json
- 5.json转换JSONObject
- 6.对象转换json字符串
- 7.对象转换json字符串格式化
1.json字符串转换成list
List<String> strings = JSONUtil.toList(jsonArray, String.class);
2.json字符串转换成对象
Price price = JSONUtil.toBean(json, Price.class);
3.json转换xml
String toXmlStr = JSONUtil.toXmlStr(jsonString);
4.xml转换json
String xmlStr = "<sfzh>123</sfzh><sfz>456</sfz><name>aa</name><gender>1</gender>";
JSONObject json = JSONUtil.parseFromXml(xmlStr);
5.json转换JSONObject
JSONObject jsonObject = JSONUtil.parseObj(toJsonStr);
6.对象转换json字符串
String toJsonStr = JSONUtil.toJsonStr(hashMap);
7.对象转换json字符串格式化
String formattingJsonStr = JSONUtil.toJsonPrettyStr(hashMap);