【整理】关于JSON转换XML

JSON-->XML:
  
  
  
  
import net.sf.json.JSON; import net.sf.json.JSONSerializer; import net.sf.json.xml.XMLSerializer; public class JsonUtil { public static String getXmlStr(String jsonStr) { jsonStr = "{'username':'gaoa4aaaa', 'password':'haha', 'nested':{'f':4, 'ss':33}}"; JSON json = JSONSerializer.toJSON(jsonStr); XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setTypeHintsEnabled( false ); xmlSerializer.setRootName("body" ); String xml = xmlSerializer.write( json ); System.out.println(xml); return xml; } public static void main(String[] args) { getXmlStr(""); } }


XML-->JSON:
JSONArray json = (JSONArray) new XMLSerializer().read(xml);
System.out.println(json);

所依赖的包:json-lib-2.3-jdk15.jar,commons-collections.jar,commons- lang.jar,commons-logging.jar,commons-beanutils.jar,ezmorph-1.0.6.jar,xom-1.1.jar

你可能感兴趣的:(xml,json,String,Class,import,nested)