Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery cookie插件读取JSON对象_Javascript_Cookies_Jquery Plugins - Fatal编程技术网

Javascript jQuery cookie插件读取JSON对象

Javascript jQuery cookie插件读取JSON对象,javascript,cookies,jquery-plugins,Javascript,Cookies,Jquery Plugins,我正在从PHP将一些数据保存在assoc数组中。数组中放入了一些Id,然后用json_编码: $ids = array(id => id, id2 => id2); json_encode($ids); store in the cookie ... 我将此插件用于jQuery: 这是字符串,存储在cookie中的值为“xxx” 当我使用这个时: var test = $.cookie( 'xxx'); 我只收到对象作为返回 如何读取此数组?JSON和JavaScript不支持“

我正在从PHP将一些数据保存在assoc数组中。数组中放入了一些Id,然后用json_编码:

$ids = array(id => id, id2 => id2);
json_encode($ids);
store in the cookie ...
我将此插件用于jQuery:

这是字符串,存储在cookie中的值为“xxx”

当我使用这个时:

var test = $.cookie( 'xxx');
我只收到
对象
作为返回


如何读取此数组?

JSON和JavaScript不支持“关联”数组。它们的等价物是

它们是经过排序的集合,索引范围为
0
length-1
,可以从非关联数组生成

<?php echo json_encode(array('foo', 'bar')); ?>
注:

  • JavaScript
    数组
    s在实例化后可以被赋予非数字键,尽管这些键不会计入
    长度

  • 除此之外:要将
    cookie
    视为
    对象
    数组
    ,您需要使用或解析它


    JSON和JavaScript不支持“关联”
    Array
    s1。它们的等价物是

    它们是经过排序的集合,索引范围为
    0
    length-1
    ,可以从非关联数组生成

    <?php echo json_encode(array('foo', 'bar')); ?>
    
    注:

  • JavaScript
    数组
    s在实例化后可以被赋予非数字键,尽管这些键不会计入
    长度

  • 除此之外:要将
    cookie
    视为
    对象
    数组
    ,您需要使用或解析它


    由于使用json对值进行编码,因此需要对其进行解码/寻址,而不是简单的值。您必须尝试以字符串的形式访问它,它会告诉您“我是一个对象”。或者,由于使用json对值进行了编码,因此需要对其进行解码/寻址,而不是简单的值。您必须尝试以字符串的形式访问它,它会告诉您“我是一个对象”。或
    { "id": "foo", "id2": "bar" }
    
    <?php echo json_encode(array('foo', 'bar')); ?>
    
    [ "foo", "bar" ]
    
    var test = JSON.parse($.cookie('xxx'));
    
    console.log(test.id);
    console.log(test.id2);