您的位置:首页 > Web前端 > JavaScript

json与xml的相互转换

2017-07-05 16:39 218 查看
很多人遇到接口调试返回数据或者请求数据为XML格式,而自己公司使用的数据交互却是json格式的 这里就对xml与json相互转换写个小方法。

import net.sf.json.JSONObject;

import net.sf.json.xml.XMLSerializer;

/**

 *

* <p>Title: JSON-XML转换工具</p>

* <p>desc:

* <p>Copyright: Copyright(c)Gb 2012</p>

* @author http://www.ij2ee.com
* @time 上午8:20:40

* @version 1.0

* @since

 */

public class XmlJSON {

    private static final String STR_JSON = "{\"name\":\"Michael\",\"address\":{\"city\":\"Suzou\",\"street\":\" Changjiang Road \",\"postcode\":100025},\"blog\":\"http://www.ij2ee.com\"}";

    public static String xml2JSON(String xml){

        return new XMLSerializer().read(xml).toString();

    }

     

    public static String json2XML(String json){

        JSONObject jobj = JSONObject.fromObject(json);

        String xml =  new XMLSerializer().write(jobj);

        return xml;

    }

     

    public static void main(String[] args) {

        TestMeetingInterface tm=new TestMeetingInterface();

        String xmlInfo =tm.getXmlInfo("C:/Users/Administrator/Desktop/fude/车单保费计算请求.xml");              

        String json = xml2JSON(xmlInfo);

        System.out.println("json="+json);

        String xml = json2XML(json);

        System.out.println("xml = "+xml);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json xml转换