Org-json-http
提供:Dev Guides
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.finddevguides.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.finddevguides.com/\" HTTP/1.1";
//Case 2: Converts Header String to JSONObject
System.out.println(HTTP.toJSONObject(headerText));
}
}
出力
POST "http://www.finddevguides.com/" HTTP/1.1
{"Request-URI":"http://www.finddevguides.com/","Method":"POST","HTTP-Version":"HTTP/1.1"}