public class XMLExchangeJson {
private static final String STR_JSON = "{\"name\":\"Michael\",\"address\":{\"city\":\"Suzou\",\"street\":\" Changjiang Road \",\"postcode\":100025},\"blog\":\"http://www.ij2ee.com\"}";
/**
* @param args
*/
public static void main(String[] args) {
String xml = jsonToXML(STR_JSON);
System.out.println(xml);
String json = xmlToJson(xml).toString();
System.out.println(json);
}
public static String jsonToXML(String json) {
JSONObject jsonObj = JSONObject.fromObject(json);
String xml = new XMLSerializer().write(jsonObj);
return xml;
}
public static JSON xmlToJson(String xml) {
return new XMLSerializer().read(xml);
}
}
寫了2個小方法,用於xml和json之間的相互轉換。
在用之前需要下載相關jar包。
jar已經上傳到我的資源下,可以免費下載。