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);
}
}
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与java、xml相互转换——json-lib
- Json-lib 实现 json,java 对象,xml 之间相互转换
- XStream在JavaBean与XML/JSON 之间相互转换
- 研究一下 项目 webservice xml和java对象的相互自动转换,json和java对象的相互自动转换
- JavaBean、xml、json、map相互转换
- xStream框架完美实现Java对象和xml文档JSON、XML相互转换
- 使用 XStream实现JavaBean 与 XML/JSON 之间相互转换
- xml和json相互转换的代码
- xstream:实现JavaBean与XML/JSON的相互转换
- 用php实现xml与json之间的相互转换
- C# XML与Json之间的相互转换
- JAVA 实现XML与JSON 相互转换
- C# XML与Json之间相互转换实例详解
- javaBean与xml、json之间的相互转换
- 对象和XML,JSON格式字符串的相互转换
- php xml与json间的相互转换
- xml和JSON格式相互转换的Java实现
- JSON ,map,list.xml相互转换
- XML 之 与Json或String的相互转换
- JavaBean、JSON、XML的相互转换,JSON-lib(2.4)