Org.Json - CSV Examples
Org.Json - Cookie Examples
Org.Json - HTTP Header Examples
Org.Json - JSON Examples
Org.Json - Property Examples
Org.Json - XML Examples
Org.Json - Exception Handling
Org.Json - Useful Resources
Org.Json - Cookie Class
Cookie class provides static methods to convert web browser's cookie text into a JSONObject, and vice versa.
Following methods are covered in the example.
toJSONObject(String) − Converts a cookie text to JSONObject Object.
toString(JSONObject) − Converts a JSONObject to cookie text.
Example - Cookie to Json Object Conversion
JsonDemo.java
package com.tutorialspoint;
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 2026 12:00:00 UTC; path = /";
//Case 1: Converts Cookie String to JSONObject
JSONObject jsonObject = Cookie.toJSONObject(cookie);
System.out.println(jsonObject);
}
}
Output
{"path":"/","expires":"Thu, 15 Jun 2026 12:00:00 UTC","name":"username","value":"Mark Den"}
Example - Json Object to Cookie Conversion
JsonDemo.java
package com.tutorialspoint;
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 2026 12:00:00 UTC; path = /";
JSONObject jsonObject = Cookie.toJSONObject(cookie);
//Case 2: Converts JSONObject to Cookie String
System.out.println(Cookie.toString(jsonObject));
}
}
Output
username=Mark Den;path=/;expires=Thu, 15 Jun 2026 12:00:00 UTC
Advertisements