Skip to content Skip to sidebar Skip to footer

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" Arrays. Their equivalent is an Object.

<?phpecho json_encode(array(id => 'foo', id2 => 'bar')); ?>
{"id":"foo","id2":"bar"}

Their Arrays 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:

  1. JavaScript Arrays can be given non-numeric keys after they've been instantiated, though such keys will not be counted in the length.

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"