我想將json字符串轉換爲使用json lib-2.4.jar的xml,我得到以下異常。螺紋JSON XML轉換問題 - 空格
異常「主要」 nu.xom.IllegalNameException:0x20的是不是一個合法的NCName字符 在nu.xom.Verifier.throwIllegalNameException(來源不明)
我已經確定這個問題是有空白字符(0x20)在json字符串的'location'標記中,並且沒有正確解析。我已經刪除了空間並進行了一次測試,然後按預期工作。我也曾嘗試不同的方式使用StringEscapeUtils.escapeXml和一些自定義的方法,如下面
StringBuffer s1 = new StringBuffer();
for (char c : value.toCharArray()) {// value is the json string
// System.out.println(c);
s1.append(c);
if (c == 0x20) {
s1.append(" ");
}
}
,但這些方法都沒有給出有預期的結果刪除空格字符。
我也在下面附上Json字符串。
{
"ArticleList": [
{
"hash": "4b8e81ec1197aa071eb62238465e4cf4",
"data":{"twitter":{"id":"276992360028712960","text":"{James 2:13} Mercy triumphs over judgment.","source":"<a href=\"http://www.abcd.com\" rel=\"nofollow\">TweetCaster<\/a>","created_at":"Fri, 07 Dec 2012 10:11:39 +0000","user":{"id":12121,"location":"{GALATIANS 2:20}","friends_count":153,"description":"Drafter#Photographer","name":"Ervin","created_at":"Sat, 28 Jan 2012 00:26:08 +0000","screen_name":"E_doubleU","id_str":"476305028","statuses_count":1212,"lang":"en","followers_count":114}},"salience":{"content":{"sentiment":0}},"klout":{"score":31},"interaction":{"id":"1e240567d69faf80e","content":"{James 2:13} because Mercy triumphs over judgment.","schema":{"version":3},"author":{"id":4763,"username":"E_doubleU","link":"http://twitter.com/","name":"Ervin wooddruff","avatar":"http://a0.twimg.com/profile_images/2921703982/13ff21c643da6b748f279baee376f58c_normal.jpeg"},"source":"TweetCaster Android","link":"http://twitter.com/","created_at":"Fri, 07 Dec 2012 10:11:39 +0000","type":"twitter"},"language":{"tag":"en","confidence":90},"demographic":{"gender":"male"}}}
]
}
任何幫助就這將是非常可觀的
看起來像庫中的一個bug。 'location'屬性的值是一個字符串,所以沒有理由爲什麼空間應該成爲問題。它看起來很困惑,因爲字符串以'{'開頭,所以它認爲它是一個對象。但由於它在引號中,所以大括號沒有任何特殊含義。 – Barmar
通過鏈接查看一些解決方法[json-lib](http://stackoverflow.com/questions/30641966/remove-white-space-from-xml-element-using-java/30665244#30665244),[gson](http ://stackoverflow.com/questions/30228343/replace-whitespace-in-json-keys)爲不同的json庫 – hsnkhrmn