Org-json-xml

提供:Dev Guides
移動先:案内検索

org.json-XML

XMLクラスは、XMLテキストをJSONObjectに、またはその逆に変換する静的メソッドを提供します。

この例では、次の方法について説明します。

  • * toJSONObject(String)*-XMLをJSONArrayオブジェクトに変換します。
  • * toString(JSONObject)*-JSONObjectオブジェクトからXMLを提供します。

import org.json.JSONObject;
import org.json.XML;

public class JSONDemo {
   public static void main(String[] args) {
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("Name", "Robert");
      jsonObject.put("ID", 1);
      jsonObject.put("Fees", new Double(1000.21));
      jsonObject.put("Active", new Boolean(true));
      jsonObject.put("Details", JSONObject.NULL);

     //Convert a JSONObject to XML
      String xmlText = XML.toString(jsonObject);
      System.out.println(xmlText);

     //Convert an XML to JSONObject
      System.out.println(XML.toJSONObject(xmlText));
   }
}

出力

<Active>true</Active><Details>null</Details><ID>1</ID><Fees>1000.21</Fees><Name>Robert</Name>
{"Active":true,"Details":null,"ID":1,"Fees":1000.21,"Name":"Robert"}