xml與json轉化

代碼比較簡單,不用介紹了。不過說一點本身遇到的問題,xml轉化json時,若是前端需讀取json須要注意一下,有的字段在xml沒有時,轉化的json也是沒有的。前端

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180130</version>
</dependency>
public static void main(String[] args) {
        //xml轉換json
        String xml="<?xml version=\"1.0\" encoding=\"GBK\"?><報告><緣由>測試</緣由></報告>";
        JSONObject jsonObject = XML.toJSONObject(xml);
        String json = jsonObject.toString();
        System.out.println(json);

        //json轉換xml
        JSONObject jsonObject1= new JSONObject(json);
        System.out.println(XML.toString(jsonObject1));
    }
相關文章
相關標籤/搜索