Saturday 16 March 2019

Convert cookie to json object


'Cookie.toJSONObject' converts a cookie to json object.

Example
String cookie = "username=Krishna; expires=Thu, 23 May 2021 12:00:00 UTC; path=/";
JSONObject jsonObject = Cookie.toJSONObject(cookie);

Above snippet generates below json string.

{
         "path": "/",
         "expires": "Thu, 23 May 2021 12:00:00 UTC",
         "name": "username",
         "value": "Krishna"
}

App.java
package com.sample;

import org.json.Cookie;
import org.json.JSONObject;

public class App {

 public static void main(String args[]) {
  String cookie = "username=Krishna; expires=Thu, 23 May 2021 12:00:00 UTC; path=/";
  JSONObject jsonObject = Cookie.toJSONObject(cookie);
   
  System.out.println(jsonObject);

 }
}

Output
{"path":"/","expires":"Thu, 23 May 2021 12:00:00 UTC","name":"username","value":"Krishna"}



Previous                                                 Next                                                 Home

No comments:

Post a Comment