簡體   English   中英

將JSON轉換為XML的問題

[英]Issues in converting JSON to 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代碼。 例如,在我的XML中,“ <”變成&lt,而“>”變成“&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">&lt;p&gt;This is demo&lt;/p&gt;</title>
<index type="string">&lt;li&gt;Demo1&lt;/li&gt;&lt;li&gt;Demo2&lt;/li&gt;</index>

預期輸出為:

<title><p>This is demo</p></title>
<index><li>Demo1</li><li>Demo2</li></index>

有什么辦法可以實現?

只需替換HTML轉義字符,它將正常工作:)

public static String filterDecode(String url){

   url = url.replaceAll("&amp;", "&")
     .replaceAll("&lt;", "<")
     .replaceAll("&gt;", ">")
     .replaceAll("&apos;", "\'")
     .replaceAll("&quot;", "\"")
     .replaceAll("&nbsp;", " ")
     .replaceAll("&copy;", "@")
     .replaceAll("&reg;", "?");
   return url;
 }

我建議您使用JSON.org,只需執行以下操作就很容易使用:

import org.json.XML;
import org.json.JSONException;
import org.json.JSONObject;

JSONObject jsonObj = new JSONObject(your string or file *.json);
System.out.println(XML.toString(jsonObj);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM