English 中文(简体)
org.json - JSONML
  • 时间:2024-12-22

org.json - JSONML


Previous Page Next Page  

JSONML class provides static methods to convert a XML text into a JSONArray, and vice versa.

Following methods are covered in the example.

    toJSONArray(String) − Converts a XML to JSONArray Object.

    toJSONObject(String) − Converts a XML to JSONObject Object.

    toString(JSONArray) − Gives a XML from a JSONArray Object.

    toString(JSONObject) − Gives a XML from a JSONObject Object.

Example

import org.json.JSONArray;
import org.json.JSONML;
import org.json.JSONObject;

pubpc class JSONDemo {
   pubpc static void main(String[] args) {
      JSONArray pst = new JSONArray();
      pst.put("name");
      pst.put("Robert");     

      System.out.println("XML from a JSONArray: ");
      String xml = JSONML.toString(pst);
      System.out.println(xml);

      System.out.println("JSONArray from a XML: ");
      pst = JSONML.toJSONArray(xml);
      System.out.println(pst);

      System.out.println("JSONObject from a XML: ");
      JSONObject object = JSONML.toJSONObject(xml);
      System.out.println(object);

      System.out.println("XML from a JSONObject: ");
      xml = JSONML.toString(object);
      System.out.println(xml);
   }
}

Output

XML from a JSONArray: 
<name>Robert</name>
JSONArray from a XML: 
["name","Robert"]
JSONObject from a XML: 
{"childNodes":["Robert"],"tagName":"name"}
XML from a JSONObject: 
<name>Robert</name>
Advertisements