设置 jsonObject 的标题


Set Title for JsonObject

这太基本了,但我尝试谷歌,但很难解释我想要搜索的关键字。

这是我的杰森

{
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);

请帮助我任何帮助将不胜感激!!

试试这个:

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);