【问题标题】:Template for JSON data?JSON数据的模板?
【发布时间】:2009-07-13 14:40:58
【问题描述】:

我找到了这个?这是最好的方法吗?

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

我希望使用某种重复循环,其中包含抛出 JSON 数据的变量。

我正在使用 Codeignitor 和 jquery。

谢谢

【问题讨论】:

  • 例子是ASP,但是你用的是PHP,“最好的方法”是什么意思?
  • 函数是jquery,在php中设计加载json数据的模板最好的方法。

标签: php ajax json codeigniter templates


【解决方案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
        )

)

【讨论】:

猜你喜欢
  • 2018-10-02
  • 2013-04-11
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多