json与xml的相互转换
生活随笔
收集整理的這篇文章主要介紹了
json与xml的相互转换
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
很多人遇到接口調(diào)試返回?cái)?shù)據(jù)或者請(qǐng)求數(shù)據(jù)為XML格式,而自己公司使用的數(shù)據(jù)交互卻是json格式的 這里就對(duì)xml與json相互轉(zhuǎn)換寫個(gè)小方法。
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
/**
?*
* <p>Title: JSON-XML轉(zhuǎn)換工具</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/車單保費(fèi)計(jì)算請(qǐng)求.xml");???????? ??? ?
??????? String json = xml2JSON(xmlInfo);
??????? System.out.println("json="+json);
??????? String xml = json2XML(json);
??????? System.out.println("xml = "+xml);
??? }
}
總結(jié)
以上是生活随笔為你收集整理的json与xml的相互转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenShift 4 - 使用 Deb
- 下一篇: JavaScript之简繁体转化代码示例