为JsonObject设置标题

时间:2013-10-09 03:14:26

标签: php mysql json

这太基础了,但我试图谷歌,但很难解释我要搜索的关键词。

这是我的Json

{
people: [
{
id: "1",
fname: "sadas",
lname: "asdad",
age: "12"
},
{
id: "2",
fname: "dfdfdf",
lname: "dfdfdf",
age: "334"
},
{
id: "3",
fname: "sdf",
lname: "sdf",
age: "343"
},
{
id: "4",
fname: "dsfsdf",
lname: "sdfsd",
age: "4"
}
],
success: 1
}

我需要在每个json对象之前有类似标题的东西。喜欢这个

{
people: [
person: {
id: "1",
fname: "sadas",
lname: "asdad",
age: "12"
},
person:{
id: "2",
fname: "dfdfdf",
lname: "dfdfdf",
age: "334"
},
person:{
id: "3",
fname: "sdf",
lname: "sdf",
age: "343"    
},
person:{
id: "4",
fname: "dsfsdf",
lname: "sdfsd",
age: "4"
}
],
success: 1
}

这是我的PHP代码

while ($row = mysql_fetch_array($result)) {
 $people= array();

$people["id"] = $row["id"];
$people["fname"] = $row["fname"];
$people["lname"] = $row["lname"];
$people["age"] = $row["age"];

array_push($response["people"] , $people);
}


$response["success"] = 1;

echo json_encode($response);

请帮帮我 任何帮助将非常感谢!!!

1 个答案:

答案 0 :(得分:0)

试试这个:

while ($row = mysql_fetch_array($result)) {
    $person = array();   
    $person["id"] = $row["id"];
    $person["fname"] = $row["fname"];
    $person["lname"] = $row["lname"];
    $person["age"] = $row["age"];
    array_push($response["people"] , array('person'=>$person));
}
$response["success"] = 1;
echo json_encode($response);