org.json - 概述
org.json 或 JSON-Java是一个简单的基于 Java 的 JSON 工具包。您可以使用 org.json 对 JSON 数据进行编码或解码。
特征
-
规范兼容- JSON.simple 完全符合 JSON 规范 - RFC4627。
-
轻量级- 它的类很少,并提供必要的功能,如编码/解码和转义 json。
-
XML Conversion - 它提供从 JSON 到 XML 的转换功能,反之亦然。
-
HTTP 标头- 支持将 HTTP 标头转换为 JSON,反之亦然。
-
Cookie - 支持将 Cookie 转换为 JSON,反之亦然。
-
CDL - 支持将逗号分隔的列表转换为 JSON,反之亦然。
-
无依赖- 无外部库依赖。可以独立包含。
org.json - CDL
CDL 类提供静态方法将逗号分隔的文本转换为 JSONArray,反之亦然。
示例中介绍了以下方法。
-
rowToJSONArray(String) - 将逗号分隔的文本转换为 JSONArray 对象。
-
rowToString(JSONArray) - 将 JSONArray 转换为逗号分隔的文本。
-
toJSONArray(String) - 将多行逗号分隔的文本转换为 JSONArray 对象的对象。
-
toJSONArray(JSONArray, String) - 将 JSONArray 对象和逗号分隔的文本转换为 JSONArray 对象。
例子
import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONTokener;
public class JSONDemo {
public static void main(String[] args) {
String csvData = "INDIA, UK, USA";
//Case 1: CSV to JSON Array
JSONArray jsonArray = CDL.rowToJSONArray(new JSONTokener(csvData));
System.out.println(jsonArray);
//Case 2: JSONArray to CSV
System.out.println(CDL.rowToString(jsonArray));
//Case 3: CSV to JSONArray of Objects
csvData = "empId, name, age \n" +
"1, Mark, 22 \n" +
"2, Robert, 35 \n" +
"3, Julia, 18";
System.out.println(CDL.toJSONArray(csvData));
//Case 4: CSV without header
jsonArray = new JSONArray();
jsonArray.put("empId");
jsonArray.put("name");
jsonArray.put("age");
csvData = "1, Mark, 22 \n" + "2, Robert, 35 \n" + "3, Julia, 18";
System.out.println(CDL.toJSONArray(jsonArray,csvData));
}
}
输出
["INDIA","UK","USA"]
INDIA,UK,USA
[{"name":"Mark","empId":"1","age":"22"},
{"name":"Robert","empId":"2","age":"35"},
{"name":"Julia","empId":"3","age":"18"}]
[{"name":"Mark","empId":"1","age":"22"},
{"name":"Robert","empId":"2","age":"35"},
{"name":"Julia","empId":"3","age":"18"}]
org.json - Cookie
Cookie 类提供静态方法将 Web 浏览器的 cookie 文本转换为 JSONObject,反之亦然。
示例中介绍了以下方法。
-
toJSONObject(String) - 将 cookie 文本转换为 JSONObject 对象。
-
toString(JSONObject) - 将 JSONObject 转换为 cookie 文本。
例子
import org.json.Cookie;
import org.json.JSONObject;
public class JSONDemo {
public static void main(String[] args) {
String cookie = "username = Mark Den; expires = Thu, 15 Jun 2020 12:00:00 UTC; path = /";
//Case 1: Converts Cookie String to JSONObject
JSONObject jsonObject = Cookie.toJSONObject(cookie);
System.out.println(jsonObject);
//Case 2: Converts JSONObject to Cookie String
System.out.println(Cookie.toString(jsonObject));
}
}
输出
{"path":"/","expires":"Thu, 15 Jun 2020 12:00:00 UTC","name":"username","value":"Mark Den"}
username=Mark Den;expires=Thu, 15 Jun 2020 12:00:00 UTC;path=/
org.json - CookieList
CookieList 类提供静态方法将 Cookie List 转换为 JSONObject,反之亦然。Cookie 列表是一系列名称/值对。
示例中介绍了以下方法。
-
toJSONObject(String) - 将 cookie 列表文本转换为 JSONObject 对象。
-
toString(JSONObject) - 将 JSONObject 转换为 cookie 列表文本。
例子
import org.json.Cookie;
import org.json.CookieList;
import org.json.JSONObject;
public class JSONDemo {
public static void main(String[] args) {
String cookie = "username = Mark Den; expires = Thu, 15 Jun 2020 12:00:00 UTC; path = /";
//Case 1: Converts Cookie String to JSONObject
JSONObject cookieJSONObject = Cookie.toJSONObject(cookie);
JSONObject cookielistJSONObject = new JSONObject();
cookielistJSONObject.put(cookieJSONObject.getString("name"),
cookieJSONObject.getString("value"));
String cookieList = CookieList.toString(cookielistJSONObject);
System.out.println(cookieList);
System.out.println(CookieList.toJSONObject(cookieList));
}
}
输出
username=Mark Den
{"username":"Mark Den"}
org.json - HTTP
HTTP 类提供静态方法将 Web 浏览器的标题文本转换为 JSONObject,反之亦然。
示例中介绍了以下方法。
-
toJSONObject(String) - 将标题文本转换为 JSONObject 对象。
-
toString(JSONObject) - 将 JSONObject 转换为标题文本。
例子
import org.json.HTTP;
import org.json.JSONObject;
public class JSONDemo {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("Method", "POST");
jsonObject.put("Request-URI", "http://www.tutorialspoint.com/");
jsonObject.put("HTTP-Version", "HTTP/1.1");
//Case 1: Converts JSONObject of Header to String
String headerText = HTTP.toString(jsonObject);
System.out.println(headerText);
headerText = "POST \"http://www.tutorialspoint.com/\" HTTP/1.1";
//Case 2: Converts Header String to JSONObject
System.out.println(HTTP.toJSONObject(headerText));
}
}
输出
POST "http://www.tutorialspoint.com/" HTTP/1.1
{"Request-URI":"http://www.tutorialspoint.com/","Method":"POST","HTTP-Version":"HTTP/1.1"}
org.json - JSONArray
JSONArray 是有序的值序列。它提供了通过索引访问值和放置值的方法。支持以下类型 -
-
布尔值
-
JSON数组
-
JSON对象
-
数字
-
细绳
-
JSONObject.NULL 对象
例子
import org.json.JSONArray;
import org.json.JSONObject;
public class JSONDemo {
public static void main(String[] args) {
JSONArray list = new JSONArray();
list.put("foo");
list.put(new Integer(100));
list.put(new Double(1000.21));
list.put(new Boolean(true));
list.put(JSONObject.NULL);
System.out.println("JSONArray: ");
System.out.println(list);
}
}
输出
JSONArray:
["foo",100,1000.21,true,null]
org.json - JSONML
JSONML 类提供了将 XML 文本转换为 JSONArray 的静态方法,反之亦然。
示例中介绍了以下方法。
-
toJSONArray(String) - 将 XML 转换为 JSONArray 对象。
-
toJSONObject(String) - 将 XML 转换为 JSONObject 对象。
-
toString(JSONArray) - 从 JSONArray 对象提供 XML。
-
toString(JSONObject) - 从 JSONObject 对象提供 XML。
例子
import org.json.JSONArray;
import org.json.JSONML;
import org.json.JSONObject;
public class JSONDemo {
public static void main(String[] args) {
JSONArray list = new JSONArray();
list.put("name");
list.put("Robert");
System.out.println("XML from a JSONArray: ");
String xml = JSONML.toString(list);
System.out.println(xml);
System.out.println("JSONArray from a XML: ");
list = JSONML.toJSONArray(xml);
System.out.println(list);
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);
}
}
输出
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>
org.json - JSON对象
JSONObject 类是键值对的无序集合。它提供了通过键访问值和放置值的方法。支持以下类型 -
-
布尔值
-
JSON数组
-
JSON对象
-
数字
-
细绳
-
JSONObject.NULL 对象
例子
import org.json.JSONArray;
import org.json.JSONObject;
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("Other Details", JSONObject.NULL);
JSONArray list = new JSONArray();
list.put("foo");
list.put(new Integer(100));
jsonObject.put("list",list);
System.out.println(jsonObject);
}
}
输出
{"Active":true,"Other Details":null,"ID":1,"Fees":1000.21,"list":["foo",100],"Name":"Robert"}
org.json - JSONStringer
JSONStringer 是一个实用程序类,用于快速构建符合 JSON 语法规则的 JSON 文本。JSONStringer 的每个实例都可以生成一个 JSON 文本。
例子
import org.json.JSONStringer;
public class JSONDemo {
public static void main(String[] args) {
String jsonText = new JSONStringer()
.object()
.key("Name")
.value("Robert")
.endObject()
.toString();
System.out.println(jsonText);
jsonText = new JSONStringer()
.array()
.value("Robert")
.value("Julia")
.value("Dan")
.endArray()
.toString();
System.out.println(jsonText);
jsonText = new JSONStringer()
.array()
.value("Robert")
.value("Julia")
.value("Dan")
.object()
.key("Name")
.value("Robert")
.endObject()
.endArray()
.toString();
System.out.println(jsonText);
}
}
输出
{"Name":"Robert"}
["Robert","Julia","Dan"]
["Robert","Julia","Dan",{"Name":"Robert"}]
org.json - 属性
属性类提供静态方法将属性文本转换为 JSONObject,反之亦然。
示例中介绍了以下方法。
-
toJSONObject(Properties) - 将属性数据转换为 JSONObject 对象。
-
toProperties(JSONObject) - 将 JSONObject 转换为属性对象。
例子
import java.util.Properties;
import org.json.JSONObject;
import org.json.Property;
public class JSONDemo {
public static void main(String[] args) {
Properties properties = new Properties();
properties.put("title", "This is a title text");
properties.put("subtitle", "This is a subtitle text");
System.out.println("Properties to JSON");
JSONObject jsonObject = Property.toJSONObject(properties);
System.out.println(jsonObject);
System.out.println("JSON to properties");
System.out.println(Property.toProperties(jsonObject));
}
}
输出
Properties to JSON
{"subtitle":"This is a subtitle text","title":"This is a title text"}
JSON to properties
{subtitle = This is a subtitle text, title = This is a title text}
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"}
org.json - JSONException 处理
org.json 的实用程序类在 JSON 无效的情况下抛出 JSONException。以下示例显示了如何处理 JSONException。
例子
import org.json.JSONException;
import org.json.XML;
public class JSONDemo {
public static void main(String[] args) {
try{
//XML tag name should not have space.
String xmlText = "<Other Details>null</Other Details>";
System.out.println(xmlText);
//Convert an XML to JSONObject
System.out.println(XML.toJSONObject(xmlText));
} catch(JSONException e){
System.out.println(e.getMessage());
}
}
}
输出
position: 24
Unexpected token RIGHT BRACE(}) at position 24.