json to xml

2021-06-16 07:02

阅读:419

标签:net   org   class   int   getjson   main   XML   instance   csdn   

package com.yile.test;


import org.json.XML;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;

public class Xml2JsonTest {
private static String xml = "weless";
public static void main(String[] args) {
testOrgJSon();
testXmlSerializer();
}

public static void testOrgJSon(){
org.json.JSONObject jsonObj = XML.toJSONObject(xml);
System.out.println("org.json xml2json:"+jsonObj);
org.json.JSONObject user = jsonObj.getJSONObject("root").getJSONObject("user");//org.json需去除根标签
System.out.println("用户名:"+user.getString("name"));
System.out.println("性别:"+user.getString("sex"));
}

public static void testXmlSerializer(){
XMLSerializer xmlSerializer = new XMLSerializer();
JSONObject jsonObject = (JSONObject)xmlSerializer.read(xml);
System.out.println("json-lib xml2json result: "+jsonObject);
JSONObject user = jsonObject.getJSONObject("user");
System.out.println("用户名:"+user.get("name"));
System.out.println("性别:"+(user.get("sex") instanceof String ? user.getString("sex"):""));
}

 

http://blog.csdn.net/lom9357bye/article/details/53291994

http://blog.csdn.net/lom9357bye/article/details/53291994

json to xml

标签:net   org   class   int   getjson   main   XML   instance   csdn   

原文地址:http://www.cnblogs.com/Lennyyi/p/7271540.html


评论


亲,登录后才可以留言!