前言:
我们在项目开发中,有时候调用别人提供的接口,接收到的数据是xml,这时候为了方便后续开发中的使用,需要解析xml并把数据转化成json,今天这篇文章就讲解下如何把xml转化成json,并在这个过程中容易出现的问题。
正文:
一、测试项目的搭建
1.需要引入的pom依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.0.9</version>
</dependency>
2.xml映射的对象
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Programmer{
private String name;
private Integer age;
private String sex;
private String img;
}
3.转化的工具类
public class Xml2Json {
public static <T> T convertToObject(String xml, Class<T> cla){
JSONObject object = XML.toJSONObject(xml);
String jsonData = object.toString();
System.err.println(jsonData);
//截取字符串
String jsonObject = jsonData.substring(jsonData.indexOf(":")+1, jsonData.length()-1);
//将json格式的字符串转换成CertApplyRequest格式
Gson gson = new Gson();
T request = gson.fromJson(jsonObject, cla);
return request;
}
二、复现问题
1.测试代码
public static void main(String[] args){
String s="<student name=\"zhangsan\">\n" +
" <sex>man</sex>\n" +
" <age>18</age>\n" +
"\t<img>https://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gb18030&pn=3&rn=1&di=99220&ln=30&word=%C5%ED%D3%DA%EA%CC&os=296869242,2380769407&cs=366539099,4208170291&objurl=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd8241f63c93bb0f35d6f8557cff86cb524a9b6f71178f5-3NGIpU_fw658&bdtype=0&simid=3446695705,94736452&pi=0&adpicid=0&timingneed=0&spn=0&is=0,0&fr=ala&ala=1" +
"&alatpl=adress&pos=1&oriquery=%E5%BD%AD%E4%BA%8E%E6%99%8F&hs=2&xthttps=111111</img>\n" +
"</student>\n";
System.out.println(s);
Programmer programmer = Xml2Json.convertToObject(s, Programmer.class);
System.out.println(programmer.toString());
}
2.运行结果:报错啦!
<student name="zhangsan">
<sex>man</sex>
<age>18</age>
<img>https://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gb18030&pn=3&rn=1&di=99220&ln=30&word=%C5%ED%D3%DA%EA%CC&os=296869242,2380769407&cs=366539099,4208170291&objurl=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd8241f63c93bb0f35d6f8557cff86cb524a9b6f71178f5-3NGIpU_fw658&bdtype=0&simid=3446695705,94736452&pi=0&adpicid=0&timingneed=0&spn=0&is=0,0&fr=ala&ala=1&alatpl=adress&pos=1&oriquery=%E5%BD%AD%E4%BA%8E%E6%99%8F&hs=2&xthttps=111111</img>
</student>
Exception in thread "main" cn.hutool.json.JSONException: Missing ';' in XML entity: &z at 121 [character 59 line 4]
at cn.hutool.json.JSONTokener.syntaxError(JSONTokener.java:373)
at cn.hutool.json.XMLTokener.nextEntity(XMLTokener.java:107)
at cn.hutool.json.XMLTokener.nextContent(XMLTokener.java:83)
at cn.hutool.json.XML.parse(XML.java:171)
at cn.hutool.json.XML.parse(XML.java:185)
at cn.hutool.json.XML.toJSONObject(XML.java:232)
at cn.hutool.json.XML.toJSONObject(XML.java:214)
at com.model.Xml2Json.convertToObject(Xml2Json.java:15)
at com.model.Xml2Json.main(Xml2Json.java:46)
Process finished with exit code 1
3.分析结果
一般出现上面的异常,基本都是xml里的格式或者有特殊字符才报错的,所以我们可以看下我们的xml里的参数是否有特殊字符。可以看到img标签里的图片地址有特殊字符&,所以这时候我们只要把&进行转义,就可以啦。
三、解决问题
1.修改后的测试代码:
public static void main(String[] args){
String s="<student name=\"zhangsan\">\n" +
" <sex>man</sex>\n" +
" <age>18</age>\n" +
"\t<img>https://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gb18030&pn=3&rn=1&di=99220&ln=30&word=%C5%ED%D3%DA%EA%CC&os=296869242,2380769407&cs=366539099,4208170291&objurl=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd8241f63c93bb0f35d6f8557cff86cb524a9b6f71178f5-3NGIpU_fw658&bdtype=0&simid=3446695705,94736452&pi=0&adpicid=0&timingneed=0&spn=0&is=0,0&fr=ala&ala=1" +
"&alatpl=adress&pos=1&oriquery=%E5%BD%AD%E4%BA%8E%E6%99%8F&hs=2&xthttps=111111</img>\n" +
"</student>\n";
String s1 = s.replaceAll("&", "&");
System.out.println(s);
System.out.println(s1);
Programmer programmer = Xml2Json.convertToObject(s1, Programmer.class);
System.out.println(programmer.toString());
}
2.运行结果:xml转化json成功。
<student name="zhangsan">
<sex>man</sex>
<age>18</age>
<img>https://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gb18030&pn=3&rn=1&di=99220&ln=30&word=%C5%ED%D3%DA%EA%CC&os=296869242,2380769407&cs=366539099,4208170291&objurl=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd8241f63c93bb0f35d6f8557cff86cb524a9b6f71178f5-3NGIpU_fw658&bdtype=0&simid=3446695705,94736452&pi=0&adpicid=0&timingneed=0&spn=0&is=0,0&fr=ala&ala=1&alatpl=adress&pos=1&oriquery=%E5%BD%AD%E4%BA%8E%E6%99%8F&hs=2&xthttps=111111</img>
</student>
<student name="zhangsan">
<sex>man</sex>
<age>18</age>
<img>https://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gb18030&pn=3&rn=1&di=99220&ln=30&word=%C5%ED%D3%DA%EA%CC&os=296869242,2380769407&cs=366539099,4208170291&objurl=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd8241f63c93bb0f35d6f8557cff86cb524a9b6f71178f5-3NGIpU_fw658&bdtype=0&simid=3446695705,94736452&pi=0&adpicid=0&timingneed=0&spn=0&is=0,0&fr=ala&ala=1&alatpl=adress&pos=1&oriquery=%E5%BD%AD%E4%BA%8E%E6%99%8F&hs=2&xthttps=111111</img>
</student>
{"student":{"img":"https://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gb18030&pn=3&rn=1&di=99220&ln=30&word=%C5%ED%D3%DA%EA%CC&os=296869242,2380769407&cs=366539099,4208170291&objurl=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd8241f63c93bb0f35d6f8557cff86cb524a9b6f71178f5-3NGIpU_fw658&bdtype=0&simid=3446695705,94736452&pi=0&adpicid=0&timingneed=0&spn=0&is=0,0&fr=ala&ala=1&alatpl=adress&pos=1&oriquery=%E5%BD%AD%E4%BA%8E%E6%99%8F&hs=2&xthttps=111111","sex":"man","name":"zhangsan","age":18}}
Xml2Json.Programmer(name=zhangsan, age=18, sex=man, img=https://image.baidu.com/search/detail?ct=503316480&z=0&tn=baiduimagedetail&ipn=d&cl=2&cm=1&sc=0&lm=-1&ie=gb18030&pn=3&rn=1&di=99220&ln=30&word=%C5%ED%D3%DA%EA%CC&os=296869242,2380769407&cs=366539099,4208170291&objurl=http%3A%2F%2Fhbimg.b0.upaiyun.com%2Fd8241f63c93bb0f35d6f8557cff86cb524a9b6f71178f5-3NGIpU_fw658&bdtype=0&simid=3446695705,94736452&pi=0&adpicid=0&timingneed=0&spn=0&is=0,0&fr=ala&ala=1&alatpl=adress&pos=1&oriquery=%E5%BD%AD%E4%BA%8E%E6%99%8F&hs=2&xthttps=111111)
总结:
xml转化josn报错的时候基本上都是xml的格式不正确或者是数据里有特殊字符,对了xml里的标签头不用转义,转义后反而会报错,原因在源码里。
我是阿达,一名喜欢分享知识的程序员,时不时的也会荒腔走板的聊一聊电影、电视剧、音乐、漫画,这里将来会有N位小伙伴在等你们,感兴趣的就赶紧来点击关注我把,哪里有不明白或有不同观点的地方欢迎留言。
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦