日期:2014-05-17 浏览次数:20744 次
import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;
public class Test {
public static void main(String[] args) throws Exception {
String xml = "<root><name type='type'>zhaipuhong</name><gender>male</gender><birthday><year>1970</year><month>12</month><day>17</day></birthday></root>";
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read( xml );
System.out.println( json.toString(2) );
}
}
{
"name": "zhaipuhong",
"gender": "male",
"birthday": {
"year": "1970",
"month": "12",
"day": "17"
}
}
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
public class Test {
public static void main(String[] args) throws Exception {
String xml = "<root><name type='type'>zhaipuhong</name><gender>male</gender><birthday><year>1970</year><month>12</month><day>17</day></birthday></root>";
String json = null;
JSONObject jsonObj = null;
try {
jsonObj = XML.toJSONObject(xml);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
json = jsonObj.toString(2); //有缩进
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(json);
}
}
{"root": {
"birthday": {
"month": 12,
"year": 1970,
"day": 17
},
"name": {
"content": "zhaipuhong",
"type": "type"
},
"gender": "male"
}}