在Jcookie中设置给定的json响应并检索它

时间:2011-11-15 04:56:22

标签: jquery json cookies

我有一个json字符串/ javascript列表,我想在jcookies中保存

json字符串/ javascript列表看起来像

    [  { "depot":  {  "id": "D1",  "intersection": {  "first": "Markham",  "second": "Lawrence"  },  "address": {  "number": "25",  "street": "Cougar Court",  "city": "Scarborough",  "province": "ON",  "postal_code": "M1J3C5"  }  }, "vehicle": [  {  "id": "V1",  "depot_id": "D1",  "model": "Ford Focus",  "price": "45",  "km_per_litre": "15",  "cargo_cu_m": "120",  "category": "Compact car",  "image": "https://www.avis.com/car-rental/images/global/en/rentersguide/vehicle_guide/hyundaiaccent-ca-08.gif"  }       ] }
 ]  

请告诉我如何将其存储在cookie中并从cookie中检索(转换为javascript变量)

1 个答案:

答案 0 :(得分:0)

好吧,如果您确实已经将值作为字符串提供,那么您应该能够使用jquery cookie插件https://github.com/carhartl/jquery-cookie来保存值,如下所示:

$.cookie('jsonList', jsonString);

并像这样检索它:

jsonString = $.cookie('jsonList');