0

I have a json string/ javascript list that I would like to save in jcookies

the json string/javascript list looks like

    [  { "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"  }       ] }
 ]  

Please tell me how can I store this in the cookie and retrieve this from the cookie (into a javascript variable)

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
kk1957
  • 8,246
  • 10
  • 41
  • 63
  • http://stackoverflow.com/questions/4225030/jquery-save-json-data-object-in-cookie or http://stackoverflow.com/questions/5483170/jquery-json-stringify-not-getting-the-entire-json-string-to-store-to-cookie might help u – run Nov 15 '11 at 05:13

1 Answers1

0

Well, if you really do have the value already available as a string, you should be able to use the jquery cookie plugin https://github.com/carhartl/jquery-cookie to save the value like so:

$.cookie('jsonList', jsonString);

and retrieve it like this:

jsonString = $.cookie('jsonList');
Jake Feasel
  • 16,785
  • 5
  • 53
  • 66
  • I tried that but when I try to access some value in it using dataArray[i].vehicle its says its dataArray[i].vehicle is undefined – kk1957 Nov 15 '11 at 05:57
  • well you have to deserialize the json back into a js object before you can reference it like that - jsObj = $.parseJSON(jsonString); Then you could refer to jsObj[i].vehicle. – Jake Feasel Nov 15 '11 at 06:00
  • i tried that , I get JSON.parse: unexpected character [Break On This Error] return window.JSON.parse( data ); jquery-latest.js (line 567) – kk1957 Nov 15 '11 at 06:18
  • So is your "string" really a string? Can you alert it? If so, are you sure it's valid JSON? Run it through http://jsonlint.com/ to find out. – Jake Feasel Nov 15 '11 at 17:42