JSON数据的模板?

时间:2009-07-13 14:40:58

标签: php ajax json codeigniter templates

我发现了这个?这是最好的方法吗?

http://weblogs.asp.net/dwahlin/archive/2009/05/03/using-jquery-with-client-side-data-binding-templates.aspx

我希望使用某种带有引入JSON数据的变量的重复循环。

我正在使用Codeignitor和jquery。

由于

1 个答案:

答案 0 :(得分:1)

如果您想要将JSON转换为PHP变量或对象,我认为此代码将解释它:

<强>代码:

<?
// here is an array
$myarray = array(
    'animal' => 'dog',
    'plant' => 'tree',
    'anotherArray' => array ('some' => 'data'),
);

// print out the array to show what it looks like
print_r($myarray);

// convert the array to json
$jsonArray = json_encode($myarray);

// print out the json data
print $jsonArray.'\n';

// convert the json data back into a PHP array
$phpArray = json_decode($jsonArray);

// print out the array that went from PHP to JSON, and back to PHP again
print_r($phpArray);

<强>输出:

Array
(
    [animal] => dog
    [plant] => tree
    [anotherArray] => Array
        (
            [some] => data
        )

)
{"animal":"dog","plant":"tree","anotherArray":{"some":"data"}}
stdClass Object
(
    [animal] => dog
    [plant] => tree
    [anotherArray] => stdClass Object
        (
            [some] => data
        )

)