1.JSON格式
随便写了一个,带有json数组,如下:
{
"TxnBatchNo": "20170607152322",
"TxnSeq": "1",
"CardNo": "2017000100000003",
"AAA": {
"hello": "nihao",
"hey": "hai",
"world": "hee"
},
"BBB": [{
"hello": "nihao",
"hey": "hai",
"people": [{
"d": "dog",
"c": "",
"e": [{
"hello": "nihao",
"hey": "hai",
"tiger": [{
"d": "dog",
"c": "",
"e": "elepahant" }]
}]
}]
}]
}
2.JAVA代码
public static String jsonToXmlUtil(String jsonString) {
if (!StringUtils.hasText(jsonString)) {
log.error("{}", "请求参数不能为空");
throw new BizException("EEEEEE", "请求参数不能为空");
}
JSONObject jsonObj = null;
try {
// 格式化JSON数据,将json字符串转化为JSONObject并将数据的key以字母顺序排序
jsonObj = JSON.parseObject(jsonString);
} catch (Exception e) {
log.error("{}", "请求参数格式有误");
throw new BizException("EEEEEE", "请求参数格式有误");
}
String xmlResult = null;
// 创建dom对象
Document document = DocumentHelper.createDocument();
// 设置编码格式
document.setXMLEncoding("utf-8");
// 添加父元素
Element addElement = document.addElement("Message");
// 添加子元素
Element thisElement = addElement.addElement("Public");
// 得到json数据中key的集合:[BBB, AAA, TxnSeq, TxnBatchNo, CardNo]
Set<String> keySet = jsonObj.keySet();
Map<Object, Object> map = new HashMap<Object, Object>(keySet.size());
for (String key : keySet) {
map.put(key, jsonObj.get(key));
// 将map转为JSON格式
// {"AAA":[{"hello":"nihao","hey":"hai","world":[{"c":"cat","d":"dog","e":"elepahant"}]}]}
JSONObject j = (JSONObject) JSON.toJSON(map);
Element childrenElement = thisElement.addElement(key);
// 判断此时key的value是否是json数组
Object json = JSON.toJSON(j.get(key));
if (json instanceof JSONArray) {
// 处理json数组
jsonArrayToXml((JSONArray) json, childrenElement);
// 判断此时key的value是JSONObject:{"hello":"nihao","hey":"hai","world":"oo"}
} else if (json instanceof JSONObject) {
JSONObject object = (JSONObject) JSON.toJSON(j.get(key));
for (String key2 : object.keySet()) {
Element childrenElement2 = childrenElement.addElement(key2);
// 再次判断
if (JSON.toJSON(j.get(key2)) instanceof JSONArray) {
jsonArrayToXml((JSONArray) JSON.toJSON(j.get(key2)), childrenElement2);
} else {
childrenElement2.addAttribute("value", object.getString(key2));
}
}
} else {
// 不是json数组则为key添加值
childrenElement.addAttribute("value", map.get(key).toString());
}
}
xmlResult = document.asXML();
return xmlResult;
}
/**
* 处理json数组
* @param jsonArray 传入的json数组
* @param childrenElement 上层的dom元素
*/
public static void jsonArrayToXml(JSONArray jsonArray, Element childrenElement) {
// 遍历json数组:[{"hello":"nihao","hey":"hai","world":[{"c":"cat","d":"dog","e":"elepahant"}]}]
for (int i = 0; i < jsonArray.size(); i++) {
// 以此例子为例:第一次遍历获取jsonArray.get(i)
// {"hello":"nihao","hey":"hai","world":[{"c":"cat","d":"dog","e":"elepahant"}]}
// 第二次遍历:{"c":"cat","d":"dog","e":"elepahant"}
// 将或得到的Object类型的字符串转化为json格式
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
// 继续遍历
for (String key : jsonObject.keySet()) {
Element addElement = childrenElement.addElement(key);
Object arry = JSON.toJSON(jsonObject.get(key));
if (arry instanceof JSONArray) {
// 递归
jsonArrayToXml((JSONArray) arry, addElement);
} else {
addElement.addAttribute("value", jsonObject.get(key).toString());
}
}
}
}
输出结果
<?xml version="1.0" encoding="utf-8"?>
<Message><Public><BBB><hello value="nihao"/><hey value="hai"/><people><d value="dog"/><e><hello value="nihao"/><hey value="hai"/><tiger><d value="dog"/><e value="elepahant"/><c value=""/></tiger></e><c value=""/></people></BBB><AAA><hello value="nihao"/><hey value="hai"/><world value="hee"/></AAA><TxnSeq value="1"/><TxnBatchNo value="20170607152322"/><CardNo value="2017000100000003"/></Public></Message>
总结
对于多层嵌套,最重要的就是递归
,其次就是循环逻辑
,上述代码的循环我还觉得有些问题,好像有些逻辑有些复杂了,博客写完吧,开始修改。
目前逻辑:
1. 先创建dom对象添加父元素等。
2. 得到json串的Set集合
3. 遍历Set集合
4. 将每一层的keySe
t放入map
中在转为json
5. 判断json.get(key)是JSONArray
还是JSONObject
还是键值对模式
6. 遍历json数组,然后再次判断,再递归
注:若转载请标明出处。