xml和json相互转换的代码

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已经上传到我的资源下,可以免费下载。

发布了23 篇原创文章 · 获赞 18 · 访问量 4783

猜你喜欢

转载自blog.csdn.net/guangtouwj/article/details/46741095
今日推荐