- 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
- Property 示例
- org.json - Property
- XML 示例
- org.json - XML
- 異常處理
- org.json - JSONException 處理
- org.json 有用資源
- org.json - 快速指南
- org.json - 有用資源
- org.json - 討論
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", "https://tutorialspoint.tw/");
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 \"https://tutorialspoint.tw/\" HTTP/1.1";
//Case 2: Converts Header String to JSONObject
System.out.println(HTTP.toJSONObject(headerText));
}
}
輸出
POST "https://tutorialspoint.tw/" HTTP/1.1
{"Request-URI":"https://tutorialspoint.tw/","Method":"POST","HTTP-Version":"HTTP/1.1"}
廣告