第一种https://mvnrepository.com/artifact/org.json/json
pom引入
org.json
json
20210307
代码:
public static void main(String[] args)
{
String xml =
""
+ "zh-cn "
+ "2010-04-28T18:36:00 "
+ ""
+ "0000 "
+ "整批交易全部成功! "
+ " "
+ " "
+ "2050278995200910236000001 "
+ "1 "
+ " ";
JSONObject object = XML.toJSONObject(xml);
System.out.println(object);
}
第二种Hutool — A set of tools that keep Java sweet.
pom引入比较火的hu工具类:
cn.hutool
hutool-all
5.5.1
代码跟上面一模一样:
public static void main(String[] args)
{
String xml =
""
+ "zh-cn "
+ "2010-04-28T18:36:00 "
+ ""
+ "0000 "
+ "整批交易全部成功! "
+ " "
+ " "
+ "2050278995200910236000001 "
+ "1 "
+ " ";
JSONObject object = XML.toJSONObject(xml);
System.out.println(object);
}
目前好多外部接口都返回xml数据。之前都是对象上@XmlAccessorType等注解来封装,对象很乱很麻烦。现在统一将xml转成json数据然后json转成对象,对象清晰好多。