将JSONObject与Java中的Cookie相互转换?
的JSON 是广泛使用的一个数据交换格式,是一种重量轻 和语言 无关。我们可以将转换的JSONObject到cookie的使用 的toString()方法和转换cookie来的JSONObject使用toJSONObject()的方法org.json.Cookie类。
将JSONObject转换为cookie
语法
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=/
将cookie转换为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"}热门推荐
10 官宣祝福语简短霸气
11 给孩子开学祝福语简短
12 毕业祝福语老婆生日简短
13 升班祝福语简短10字
14 给表妹生日祝福语简短
15 孩子新年祝福语简短精辟
16 父亲婚礼祝福语经典简短
17 中考祝福语给同学简短
18 结婚伴娘祝福语简短精辟