這裏使用的是耗費理解力最少的寫法,不表明最優解或最經常使用解。java
這個功能比JSON轉XML經常使用。apache
準備好,放於src根目錄下:json
<?xml version="1.0" encoding="UTF-8" ?> <bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title>哈利波特</title> <author>J·K·羅琳</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
commons-io-2.6.jar;app
org.json-2.0.jar;測試
package books; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import org.apache.commons.io.IOUtils; import org.json.JSONException; import org.json.JSONObject; import org.json.XML; public class XmlToJson { public static String xmlToJson() { try { FileInputStream inputStream = new FileInputStream(new File("src/books.xml")); String xmlStr = IOUtils.toString(inputStream,"UTF-8"); JSONObject object = XML.toJSONObject(xmlStr); return object.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { System.out.println(xmlToJson()); } }
{"bookstore":{"book":[{"year":2005,"author":"Giada De Laurentiis","price":30,"category":"COOKING","title":{"lang":"en","content":"Everyday Italian"}},{"year":2005,"author":"J·K·羅琳","price":29.99,"category":"CHILDREN","title":{"lang":"en","content":"哈利波特"}},{"year":2003,"author":"Erik T. Ray","price":39.95,"category":"WEB","title":{"lang":"en","content":"Learning XML"}}]}}
美美噠格式化以後:code
{ "bookstore": { "book": [{ "year": 2005, "author": "Giada De Laurentiis", "price": 30, "category": "COOKING", "title": { "lang": "en", "content": "Everyday Italian" } }, { "year": 2005, "author": "J·K·羅琳", "price": 29.99, "category": "CHILDREN", "title": { "lang": "en", "content": "哈利波特" } }, { "year": 2003, "author": "Erik T. Ray", "price": 39.95, "category": "WEB", "title": { "lang": "en", "content": "Learning XML" } }] } }
嗯,基本上沒什麼問題,能夠直接用了。xml
準備好,放於src根目錄下:utf-8
{ "message": "這是一箇中文測試", "info": { "fileType": "json", "fileUsage": "我在哪裏?我要作什麼?" }, "books": [{ "title": "Everyday Italian", "author": "Giada De Laurentiis", "year": "2005", "price": "30.00" }, { "title": "Harry Potter", "author": "J K. Rowling", "year": "2005", "price": "29.99" }, { "title": "Learning JSON", "author": "Erik T. Ray", "year": "2005", "price": "39.95" } ] }
commons-io-2.6.jar;開發
org.json-2.0.jar;文檔
注意這裏的jsonToXmlstr(JSONObject jsonObject, StringBuffer buffer )...
package books; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.util.Iterator; import java.util.Map; import java.util.Set; public class JsonToXml { public static String jsonToXmlstr(JSONObject jsonObject, StringBuffer buffer ){ Set<Map.Entry<String, Object>> set = jsonObject.entrySet(); Iterator<Map.Entry<String, Object>> iterator = set.iterator(); while (iterator.hasNext()){ Map.Entry<String, Object> entry = iterator.next(); if (entry.getValue().getClass().getName().equals("com.alibaba.fastjson.JSONObject")){ buffer.append("<" + entry.getKey() + ">"); JSONObject jo = jsonObject.getJSONObject(entry.getKey()); jsonToXmlstr(jo, buffer); buffer.append("</" + entry.getKey() + ">"); } else if(entry.getValue().getClass().getName().equals("com.alibaba.fastjson.JSONArray")){ JSONArray ja = jsonObject.getJSONArray(entry.getKey()); for (int i = 0; i < ja.size(); i++) { buffer.append("<" + entry.getKey() + ">"); JSONObject joChild = ja.getJSONObject(i); jsonToXmlstr(joChild, buffer); buffer.append("</" + entry.getKey() + ">"); } } else if(entry.getValue().getClass().getName().equals("java.lang.String")){ buffer.append("<" + entry.getKey() + ">" + entry.getValue()); buffer.append("</" + entry.getKey() + ">"); } } return buffer.toString(); } public static String jsonToXml() { try { String jsonStr = FileUtils.readFileToString(new File("src/books.json"), "UTF-8"); JSONObject jsonObject = JSON.parseObject(jsonStr); StringBuffer buffer = new StringBuffer(); buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); return jsonToXmlstr(jsonObject, buffer); } catch (IOException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { System.out.println(jsonToXml()); } }
注意:
這裏jsonToXmlstr方法遍歷非字符串數據類型時,可能形成數據丟失,由於葉子結點數據類型的判斷條件是equals("java.lang.String")嘛 U know~
<?xml version="1.0" encoding="utf-8"?><books><year>2005</year><author>Giada De Laurentiis</author><price>30.00</price><title>Everyday Italian</title></books><books><year>2005</year><author>J K. Rowling</author><price>29.99</price><title>Harry Potter</title></books><books><year>2005</year><author>Erik T. Ray</author><price>39.95</price><title>Learning JSON</title></books><message>這是一箇中文測試</message><info><fileUsage>我在哪裏?我要作什麼?</fileUsage><fileType>json</fileType></info>
開森噠格式化以後:
<?xml version="1.0" encoding="utf-8"?> <books> <year>2005</year> <author>Giada De Laurentiis</author> <price>30.00</price> <title>Everyday Italian</title> </books> <books> <year>2005</year> <author>J K. Rowling</author> <price>29.99</price> <title>Harry Potter</title> </books> <books> <year>2005</year> <author>Erik T. Ray</author> <price>39.95</price> <title>Learning JSON</title> </books> <message>這是一箇中文測試</message> <info> <fileUsage>我在哪裏?我要作什麼?</fileUsage> <fileType>json</fileType> </info>
——發現一個開森不起來的問題:
代碼完美地將json數據轉化爲了xml格式,可是這並非一個符合xml文檔結構的xml。
緣由:xml是樹形結構,是有根元素的。若是原生的json數據不遵循這個原則,那麼生成的xml文檔就是不規範的,須要開發者根據具體狀況改進上述java代碼,或生成結果後再二次處理。