Saturday, 16 March 2019

Convert JSONObject to a cookie string


‘Cookie.toString’ method converts a json object to cookie string.

String cookie = Cookie.toString(jsonObject);

App.java
package com.sample;

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

public class App {

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


 }
}

Output
username=krishna;expires=Thu, 23 May 2021 12:00:00 UTC;path=/

Previous                                                 Next                                                 Home

No comments:

Post a Comment