JSON은 가벼운 데이터 교환입니다. 형식 및 JSON 형식은 키-값과 같습니다. 쌍. XML을 JSON 배열로 변환할 수 있습니다. org.json.XML 클래스 사용 , 이것은 정적 메소드, XML.toJSONObject() XML을 JSON 배열로 변환합니다.
구문
public static JSONObject toJSONObject(java.lang.String string) throws JSONException
아래 예에서 XML을 JSON 배열로 변환
예시
import org.json.*;
public class ConvertXMLToJSONArrayTest {
public static String xmlString= "<?xml version=\"1.0\" ?><root><test attrib=\"jsontext1\">tutorialspoint</test><test attrib=\"jsontext2\">tutorix</test></root>";
public static void main(String[] args) {
try {
JSONObject json = XML.toJSONObject(xmlString); // converts xml to json
String jsonPrettyPrintString = json.toString(4); // json pretty print
System.out.println(jsonPrettyPrintString);
} catch(JSONException je) {
System.out.println(je.toString());
}
}
} 출력
{"root": {"test": [
{
"attrib": "jsontext1",
"content": "tutorialspoint"
},
{
"attrib": "jsontext2",
"content": "tutorix"
}
]}}