如何在json中传递变量数组并检索它

时间:2014-03-26 04:52:40

标签: php mysql json zend-framework

我有一个以变量$ coinPacks命名的数组,我试图像这样传递它


echo $this->_helper->json(array('pack'=>$coinPacks));

但是当我使用此代码显示它时什么都没有


 alert(JSON.stringify(response.pack));

我如何传递值并检索它


当我打印变量$ CoinPacks时,我得到一个像这样的回复

[_ data:protected] =>排列         (             [0] =>排列                 (                     [id] => 1                     [number_of_coins] => 100                     [price] => 1.00                 )

        [1] => Array
            (
                [id] => 2
                [number_of_coins] => 500
                [price] => 3.00
            )

        [2] => Array
            (
                [id] => 3
                [number_of_coins] => 1000
                [price] => 5.00
            )

        [3] => Array
            (
                [id] => 4
                [number_of_coins] => 5000
                [price] => 10.00
            )

    )

也像这些代码一样

[_ metadata:protected] =>排列                 (                     [id] =>排列                         (                             [SCHEMA_NAME] =>                             [TABLE_NAME] =>包                             [COLUMN_NAME] => ID                             [COLUMN_POSITION] => 1                             [DATA_TYPE] => INT                             [DEFAULT] =>                             [NULLABLE] =>                             [LENGTH] =>                             [SCALE] =>                             [PRECISION] =>                             [UNSIGNED] =>                             [PRIMARY] => 1                             [PRIMARY_POSITION] => 1                             [IDENTITY] => 1                         )

                [pack_name] => Array
                    (
                        [SCHEMA_NAME] => 
                        [TABLE_NAME] => packs
                        [COLUMN_NAME] => pack_name
                        [COLUMN_POSITION] => 2
                        [DATA_TYPE] => varchar
                        [DEFAULT] => 
                        [NULLABLE] => 1
                        [LENGTH] => 200
                        [SCALE] => 
                        [PRECISION] => 
                        [UNSIGNED] => 
                        [PRIMARY] => 
                        [PRIMARY_POSITION] => 
                        [IDENTITY] => 
                    )

                [number_of_coins] => Array
                    (
                        [SCHEMA_NAME] => 
                        [TABLE_NAME] => packs
                        [COLUMN_NAME] => number_of_coins
                        [COLUMN_POSITION] => 3
                        [DATA_TYPE] => int
                        [DEFAULT] => 
                        [NULLABLE] => 1
                        [LENGTH] => 
                        [SCALE] => 
                        [PRECISION] => 
                        [UNSIGNED] => 
                        [PRIMARY] => 
                        [PRIMARY_POSITION] => 
                        [IDENTITY] => 
                    )

                [price] => Array
                    (
                        [SCHEMA_NAME] => 
                        [TABLE_NAME] => packs
                        [COLUMN_NAME] => price
                        [COLUMN_POSITION] => 4
                        [DATA_TYPE] => float
                        [DEFAULT] => 
                        [NULLABLE] => 1
                        [LENGTH] => 
                        [SCALE] => 2
                        [PRECISION] => 10
                        [UNSIGNED] => 
                        [PRIMARY] => 
                        [PRIMARY_POSITION] => 
                        [IDENTITY] => 
                    )

1 个答案:

答案 0 :(得分:0)

PHP

json_encode(array('pack'=>$coinPacks)); // check if it matches the output of your helper

这会将数组编码为JSON。


JS

console.log(JSON.parse(response));

这会将JSON解码为JS对象(并将其输出到控制台)。

获取您的包信息:

var pack = JSON.parse(response).pack;