- org.json 教程
- org.json - 主页
- org.json - 概述
- org.json - 环境设置
- CSV 示例
- org.json-CDL
- Cookie 示例
- org.json - Cookie
- org.json - CookieList
- HTTP 标头示例
- org.json - HTTP
- JSON 示例
- org.json - JSONArray
- org.json - JSONML
- org.json - JSONObject
- org.json - JSONStringer
- 属性示例
- org.json - 属性
- XML 示例
- org.json - XML
- 异常处理
- org.json - JSON异常处理
- org.json 有用资源
- org.json - 快速指南
- org.json - 有用的资源
- org.json - 讨论
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"}