- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert JSONObject to/from a cookie in Java?
The JSON is one of the widely used data-interchange formats and is a lightweight and language independent. We can convert a JSONObject to cookie using the toString() method and convert a cookie to JSONObject using the toJSONObject() method of org.json.Cookie class.
Convert JSONObject to cookie
Syntax
public static java.lang.String toString(JSONObject jo) throws JSONException
Example
import org.json.Cookie; import org.json.JSONObject; public class JSONObjectToCookieTest { public static void main(String args[]) { JSONObject jsonObject = new JSONObject(); jsonObject.put("path", "/"); jsonObject.put("expires", "Thu, 07 May 2020 12:00:00 UTC"); jsonObject.put("name", "username"); jsonObject.put("value", "Adithya"); String cookie = Cookie.toString(jsonObject); System.out.println(cookie); } }
Output
username=Adithya;expires=Thu, 07 May 2020 12:00:00 UTC;path=/
Convert cookie to JSONObject
Syntax
public static JSONObject toJSONObject(java.lang.String string) throws JSONException
Example
import org.json.Cookie; import org.json.JSONObject; public class ConvertCookieToJSONObjectTest { public static void main(String args[]) { String cookie = "username=Adithya; expires=Thu, 07 May 2020 12:00:00 UTC; path=/"; JSONObject jsonObject = Cookie.toJSONObject(cookie); System.out.println(jsonObject); } }
Output
{"path":"/","expires":"Thu, 07 May 2020 12:00:00 UTC","name":"username","value":"Adithya"}
- Related Articles
- How can we add a JSONArray to JSONObject in Java?
- How can we sort a JSONObject in Java?
- How to auto-increment the property of a JSONObject in Java?
- How can we add a JSONArray within JSONObject in Java?
- How to set a cookie and get a cookie with JavaScript?
- Importance of the accumulate() method of JSONObject in Java?
- Convert from String to long in Java
- Convert from float to String in Java
- Convert from String to float in Java
- How to use JSONobject to parse JSON in Android?
- How to read a cookie using JavaScript?
- Java Program to convert from a java.util.Date Object to a java.sql.Date Object
- Java Program to convert from integer to String
- Java Program to convert from String to integer
- Java Program to convert from decimal to binary

Advertisements