org.json - CookieList

Die CookieList-Klasse bietet statische Methoden zum Konvertieren der Cookie-Liste in JSONObject und umgekehrt. Die Cookie-Liste ist eine Folge von Name / Wert-Paaren.

Die folgenden Methoden werden im Beispiel behandelt.

  • toJSONObject(String) - Konvertiert einen Cookie-Listentext in ein JSONObject-Objekt.

  • toString(JSONObject) - Konvertiert ein JSONObject in einen Cookie-Listentext.

Beispiel

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

public class JSONDemo {
   public 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 cookielistJSONObject = new JSONObject();       
      cookielistJSONObject.put(cookieJSONObject.getString("name"), 
         cookieJSONObject.getString("value"));       

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

Ausgabe

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