1

这太基本了,但我试图用谷歌搜索,但很难解释我需要搜索的关键字是什么。

这是我的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);

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

4

1 回答 1

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);
于 2013-10-09T03:28:32.107 回答