JSONObject转XML格式 使用XMLSerializer乱码
需要把JSONObject对象转换成XML格式
jar包json-lib-2.1.jar
source:
JSONObject response = getValue(ctx," where PE.FID = '"+PersonID+"'")
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setRootName("user_info");
xmlSerializer.setTypeHintsEnabled(false);
String xml = xmlSerializer.write(response);
source中response的格式,personInfos数组中是有多个jsonobject的,我这里只拷了一个
{
"result":"ok",
"error":"",
"personInfos":[
{
"FNumber":"Selina",
"FName":"Selina",
"FEmail":"",
"FOfficePhone":"",
"FUseStatus":"1",
"FId":"Dvu/j2yqS7erc6waxcbUy4Dvfe0=",
"FLastUpdateTime":153700000,
"FPosition":[
{
"FID":"2tKR7HaITquFH7Tu0GKK4XSuYS4=",
"FLASTUPDATETIME":1537891200000
}
]
}
]
}
出来的结果中文乱码了
正常输出结果删了,拿error的凑合看
`<?xml version="1.0" encoding="UTF-8"?>
<user_info>鍒楀悕 ‘FIDleft’ 鏃犳晥銆?fail</user_info>
问题解决:
jar包json-lib-2.1.jar
改为json-lib-2.4-jdk15.jar
设置UTK-8
xmlSerializer.write(response, "UTF-8");
结果
{
"wsServer":"SHR",
"dateTime":"2021-08-2511:06:26",
"proc":"single",
"result":"OK",
"ERRORS":{
"ERR":"",
"@errCounts":"0"
},
"positions":{
"positionInfo":{
"FID":"DpUA9/pSSH6B+vCXVEIgVnSuYS4=",
"FNumber":"000326",
"FName":"财务会计",
"SuperiorID":"m6vrfXGQQdiEESKWzucN7HSuYS4=",
"FcreatedTime":"2018-09-20 22:06:42.0",
"Fstate":"1",
"ParentNumber":"",
"FLastUpdateTime":"2018-09-20 22:06:42.0"
}
},
"@DATETYPE":"POSITION"
}
乱码解决