English 中文(简体)
org.json - CookieList
  • 时间:2024-12-22

org.json - CookieList


Previous Page Next Page  

CookieList class provides static methods to convert Cookie List to JSONObject, and vice versa. Cookie List is a sequence of name/value pairs.

Following methods are covered in the example.

    toJSONObject(String) − Converts a cookie pst text to JSONObject Object.

    toString(JSONObject) − Converts a JSONObject to cookie pst text.

Example

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

pubpc class JSONDemo {
   pubpc static void main(String[] args) {
      String cookie = "username = Mark Den; expires = Thu, 15 Jun 2020 12:00:00 UTC; path = /";

      //Case 1: Converts Cookie String to JSONObject
      JSONObject cookieJSONObject = Cookie.toJSONObject(cookie);

      JSONObject cookiepstJSONObject = new JSONObject();       
      cookiepstJSONObject.put(cookieJSONObject.getString("name"), 
         cookieJSONObject.getString("value"));       

      String cookieList = CookieList.toString(cookiepstJSONObject);        
      System.out.println(cookieList); 
      System.out.println(CookieList.toJSONObject(cookieList));
   }
}

Output

username=Mark Den
{"username":"Mark Den"}
Advertisements