<< 简单介绍Oracle驱动表 - 51CTO.COM | 首页 | GeoMap/api.md at master · x6doooo/GeoMap · Raphael >>

Quickest way to convert XML to JSON in Java - Stack Overflow

The JSON in Java page on json.org has some great resources.

Looks like XML.java and JSONML.java are the classes you're looking for:

public class Main {       
public static int PRETTY_PRINT_INDENT_FACTOR = 4;      
public static String TEST_XML_STRING =         "<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";       
public static void main(String[] args) {          
try {              
JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);              
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);              
System.out.println(jsonPrettyPrintString);          
} catch (JSONException je) {              
System.out.println(je.toString());          
}      }  }

Looks like it does the job. Output is:

{"test": {     "attrib": "moretest",     "content": "Turn this to JSON" }}

Expanded from my original entry. I hope this helps.

阅读全文……

标签 : , ,



发表评论 发送引用通报