我正在編寫一個代碼,將我的JSON轉換爲XML。轉換代碼是:將JSON轉換爲XML的問題
BufferedReader in;
try {
InputStream is = new FileInputStream("/Users/appleBud/json");
jsonData = IOUtils.toString(is);
System.out.println("json data is: "+jsonData);
} catch (IOException e) {
e.printStackTrace();
}
XMLSerializer serializer = new XMLSerializer();
JSON json = JSONSerializer.toJSON(jsonData);
String xml = serializer.write(json);
System.out.println(xml);
我能夠生成XML。但是,我的JSON包含無法以各自格式顯示的HTML代碼。例如,'<'變成& lt和'>'在我的XML中變成'& gt'。我試圖使用不同的解決方案,但沒有解決我的問題。
我的JSON是一樣的東西:
{
"title":"<p>This is demo </p>",
"index":"<li>Demo1</li><li>Demo2</li>",
"name": "KB649364539",
"creationDate": "Wed Aug 07 2013 00:00:52 GMT+0000"
}
,併產生相應的XML是:
<title type="string"><p>This is demo</p></title>
<index type="string"><li>Demo1</li><li>Demo2</li></index>
預期成果是:
<title><p>This is demo</p></title>
<index><li>Demo1</li><li>Demo2</li></index>
有什麼辦法,我可以做到這一點?
那麼你的Json看起來像什麼? – Jason
'<'變成'<'和'>'變成'>'**這不是一個錯誤**,這就是它如何顯示爲 – TwilightSun
爲什麼不將Json轉換爲Java對象然後將其轉換爲XML?您可以輕鬆控制內容。 – elbuild