JSON parsing cookie contents

閲覧: 41 回
最初の未読メッセージにスキップ

Gustav van der Merwe

未読、
2015/08/18 15:45:472015/08/18
To: lif...@googlegroups.com
I've been trying to grab data out of a cookie and parse that, the value is:

{stamp:'XmooaL03kY1V1nk+nMNSUU96m9RkPyId33vL9wPmiXUvKWPEXQt8xA=='%2Cnecessary:true%2Cpreferences:true%2Cstatistics:false%2Cmarketing:false}

which after url decoding is:

{stamp:'XmooaL03kY1V1nk nMNSUU96m9RkPyId33vL9wPmiXUvKWPEXQt8xA==',necessary:true,preferences:true,statistics:false,marketing:false}

which gives me the following error:

net.liftweb.json.JsonParser$ParseException: unknown token s
Near: {st
at net.liftweb.json.JsonParser$Parser.fail(JsonParser.scala:235) ~[lift-json_2.11-3.0-M6.jar:3.0-M6]
at net.liftweb.json.JsonParser$Parser.nextToken(JsonParser.scala:322) ~[lift-json_2.11-3.0-M6.jar:3.0-M6]

Is that JSON not valid? Seems to be to my mind...

Regards,
Gustav van der Merwe

signature.asc

Antonio Salazar Cardozo

未読、
2015/08/18 16:09:322015/08/18
To: Lift、gv...@gvdm.me
No, it's not valid. It's a valid JavaScript object, but not valid JSON. JSON is very
strict: keys must be in double quotes, values that are strings must likewise be in
double quotes.

So this would be valid JSON:

{"stamp":"XmooaL03kY1V1nk nMNSUU96m9RkPyId33vL9wPmiXUvKWPEXQt8xA==","necessary":true,"preferences":true,"statistics":false,"marketing":false}

Thanks,
Antonio

Daniel Johannsen

未読、
2015/08/21 8:32:462015/08/21
To: Lift、gv...@gvdm.me
Cookiebot.com is using this format in the cookie named "cookieconsent" to store the website visitor's current cookie consent state. The reason for the used format is to support the Safari browser which still uses the original Netscape cookie specification instead of the newer RFC 6265 standard. The Netscape cookie spec does not allow valid JSON, but the cookie value can easily be converted to valid JSON using regular expressions:

PHP example:
$cookie_value = urldecode("{stamp:'XmooaL03kY1V1nk+nMNSUU96m9RkPyId33vL9wPmiXUvKWPEXQt8xA=='%2Cnecessary:true%2Cpreferences:true%2Cstatistics:false%2Cmarketing:false}");
$valid_php_json = preg_replace('/\s*:\s*([a-zA-Z0-9_]+?)([}\[,])/', ':"$1"$2', preg_replace('/([{\[,])\s*([a-zA-Z0-9_]+?):/', '$1"$2":', str_replace("'", '"',stripslashes($cookie_value))));

C# example:
String CookieValue = HttpUtility.UrlDecode("{stamp:'XmooaL03kY1V1nk+nMNSUU96m9RkPyId33vL9wPmiXUvKWPEXQt8xA=='%2Cnecessary:true%2Cpreferences:true%2Cstatistics:false%2Cmarketing:false}");
String ValidJSONString = Regex.Replace(Regex.Replace(CookieValue.Replace("'", "\""), @"([{\[,])\s*([a-zA-Z0-9_]+?):", "$1\"$2\":"), @"\s*:\s*([a-zA-Z0-9_]+?)([}\[,])", ":\"$1\"$2");

In .NET 3.5 or newer the cookie value can be deserialized directly using the JavaScriptSerializer of System.Web.Script.Serialization:

C# example:
String CookieValue = HttpUtility.UrlDecode("{stamp:'XmooaL03kY1V1nk+nMNSUU96m9RkPyId33vL9wPmiXUvKWPEXQt8xA=='%2Cnecessary:true%2Cpreferences:true%2Cstatistics:false%2Cmarketing:false}");
JavaScriptSerializer CookieConsentSerializer = new JavaScriptSerializer();
dynamic CookieConsent = CookieConsentSerializer.Deserialize<object>(CookieValue);

Visual Basic example:
Dim CookieValue As [String] = HttpUtility.UrlDecode("{stamp:'XmooaL03kY1V1nk+nMNSUU96m9RkPyId33vL9wPmiXUvKWPEXQt8xA=='%2Cnecessary:true%2Cpreferences:true%2Cstatistics:false%2Cmarketing:false}")
Dim CookieConsentSerializer As New JavaScriptSerializer()
Dim CookieConsent As dynamic = CookieConsentSerializer.Deserialize(Of Object)(CookieValue)

Cheers,
Daniel
全員に返信
投稿者に返信
転送
新着メール 0 件