The JSON เป็นหนึ่งใน การแลกเปลี่ยนข้อมูล . ที่ใช้กันอย่างแพร่หลาย รูปแบบและเป็น น้ำหนักเบา และ ภาษา อิสระ . เราสามารถแปลง JSONObject เป็นคุกกี้ โดยใช้ toString() เมธอดและแปลง คุกกี้เป็น JSONObject โดยใช้ toJSONObject() วิธีการของ org.json.Cookie ชั้นเรียน
แปลง JSONObject เป็นคุกกี้
ไวยากรณ์
public static java.lang.String toString(JSONObject jo) throws JSONException
ตัวอย่าง
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);
}
} ผลลัพธ์
username=Adithya;expires=Thu, 07 May 2020 12:00:00 UTC;path=/
แปลงคุกกี้เป็น JSONObject
ไวยากรณ์
public static JSONObject toJSONObject(java.lang.String string) throws JSONException
ตัวอย่าง
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);
}
} ผลลัพธ์
{"path":"/","expires":"Thu, 07 May 2020 12:00:00 UTC","name":"username","value":"Adithya"}