Jquery Cookie Plugin Read Json Object
I am saving from PHP some data in assoc array. There are some Id's putted in an array and then json_encoded: $ids = array(id => id, id2 => id2); json_encode($ids); store in t
Solution 1:
JSON and JavaScript don't support "associative" Array
s. Their equivalent is an Object
.
<?phpecho json_encode(array(id => 'foo', id2 => 'bar')); ?>
{"id":"foo","id2":"bar"}
Their Array
s are sorted collections with indexes from 0
to length - 1
and can be generated from a non-associative array.
<?phpecho json_encode(array('foo', 'bar')); ?>
[ "foo", "bar" ]
Note:
- JavaScript
Array
s can be given non-numeric keys after they've been instantiated, though such keys will not be counted in thelength
.
Beyond that distinction: to treat the cookie
as either an Object
or Array
, you'll need to parse it with either JSON.parse()
or $.parseJSON()
.
var test = JSON.parse($.cookie('xxx'));
console.log(test.id);
console.log(test.id2);
Post a Comment for "Jquery Cookie Plugin Read Json Object"