簡體   English   中英

如何從xml解析json時轉義新行

[英]how to escape new line while parsing json from xml

我正在學習從xml解析json,然后解析了它,但是當我從xml解析json時,我在json中得到了換行符,有人可以建議我如何刪除它,這是我的java代碼。 公共課程Test2 {

private URL url = null;
    private InputStream inputStream = null;
    private int test = 4;

    public void getXMLfromJson() {
        try {
            url = this.getClass().getClassLoader().getResource("sample.xml");
            inputStream = url.openStream();
            String xml = IOUtils.toString(inputStream);
            org.json.JSONObject JSON = XML.toJSONObject(xml);
            String json = JSON.toString(test);
            System.out.println(json);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
                url = null;
            } catch (IOException ex) {
            }
        }
    }

    public static void main(String[] args) {
        new Test2().getXMLfromJson();
    }
}

我正在閱讀的Xm​​l文件如下,

<!--?xml version="1.0" encoding="UTF-8"?-->
    <catalog>
       <book id="bk01">
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
          <publish_date>2000-10-01</publish_date>
          <description>An in-depth look at creating applications
          with XML.</description>
       </book>
       <book id="bk02">
          <author>Ralls, Kim</author>
          <title>Midnight Rain</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2000-12-16</publish_date>
          <description>A former architect battles corporate zombies,
          an evil sorceress, and her own childhood to become queen
          of the world.</description>
       </book>
    </catalog>

這是我程序的輸出結果,其中我遇到了換行符和空格,並且遇到了換行符

    {"catalog": {"book": [
    {
        "genre": "Computer",
        "id": "bk01",
        "author": "Gambardella, Matthew",
        "title": "XML Developer's Guide",
        "price": 44.95,
        "publish_date": "2000-10-01",
        "description": "An in-depth look at creating applications\n      with XML."
    },
    {
        "genre": "Fantasy",
        "id": "bk02",
        "author": "Ralls, Kim",
        "title": "Midnight Rain",
        "price": 5.95,
        "publish_date": "2000-12-16",
        "description": "A former architect battles corporate zombies,\n      an evil sorceress, and her own childhood to become queen\n      of the world."
    }
]}}

我該如何\\ n換行符和空格。

我認為您應該找到一種方法,使您的XML沒有它們。 您在上面所做的只是轉換而不是轉換。

只是分享我的解決方案。 只需替換XML中的換行符,然后再將其轉換為JSON。

//to replace line break in XML
public string escapeLineBreak(string myXml) {
   if (myXml != null && myXml != "") {
        return myXml.Replace("\r\n", "\\n");
   } else {
        return myXml;
   }
}

暫無
暫無

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

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