- org.json Tutorial
- org.json - Home
- org.json - Overview
- org.json - Environment Setup
- CSV Examples
- org.json - CDL
- Cookie Examples
- org.json - Cookie
- org.json - CookieList
- HTTP Header Examples
- org.json - HTTP
- JSON Examples
- org.json - JSONArray
- org.json - JSONML
- org.json - JSONObject
- org.json - JSONStringer
- Property Examples
- org.json - Property
- XML Examples
- org.json - XML
- Exception Handling
- org.json - JSONException Handling
- org.json Useful Resources
- org.json - Quick Guide
- org.json - Useful Resources
- org.json - Discussion
org.json - HTTP
HTTP class provides static methods to convert web browser's header text into a JSONObject, and vice versa.
Following methods are covered in the example.
toJSONObject(String) − Converts a header text to JSONObject Object.
toString(JSONObject) − Converts a JSONObject to header text.
Example
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)); } }
Output
POST "http://www.tutorialspoint.com/" HTTP/1.1 {"Request-URI":"http://www.tutorialspoint.com/","Method":"POST","HTTP-Version":"HTTP/1.1"}
Advertisements