使用mysql帮助生成json数据

时间:2011-05-17 18:21:29

标签: json

知道如何通过查询解决这个问题吗?

$obj = array(
    "attr" => array(
      "id" => 1,
      "rel" => "drive"                  
      ),
    "data" => "My Documents",

    "children" => array(
      array(
    "attr" => array(
      "id" => 2,
      "rel" => "file"
      ),
    "data" => "file1.doc",
    "state" => ""
    )
      ) 
    );

echo json_encode($obj); 

我只需要在上面代码的一部分下面循环。

array(
    "attr" => array(
      "id" => 2,
      "rel" => "file"
      ),
    "data" => "file1.doc",
    "state" => ""
    )

任何帮助都会很棒。

由于

2 个答案:

答案 0 :(得分:0)

您可以尝试编译和安装第三方JSON UDF:

http://www.mysqludf.org/lib_mysqludf_json/index.php

答案 1 :(得分:0)

$x = 0;
while($row = mssql_fetch_object($query) )
{
   $json[$x] = array    (   
                            'lastname' => $row->firstname,
                            'firstname' => $row->lastname
                        );  
   $x++;
}
echo  json_encode($json);