加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml和json相互转换的代码

发布时间:2020-12-16 06:15:48 所属栏目:百科 来源:网络整理
导读: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 */publi
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已经上传到我的资源下,可以免费下载。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读