xml和json相互转换的代码

来源:互联网 发布:centos终端能输入中文 编辑:程序博客网 时间:2024/04/30 09:09
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已经上传到我的资源下,可以免费下载。

0 0
原创粉丝点击