【发布时间】:2015-01-24 21:47:52
【问题描述】:
我正在尝试以如下格式输出我的 JSON:http://api.androidhive.info/json/movies.json,但我不确定如何通过我的 PHP 输出代码完成此操作。现在,它在顶部显示“post”,是一张我不知道如何成功删除的地图,在这里看到:http://shipstudent.com/complaint_desk/androidfriendsList.php?username=noah。如果您需要更多信息,请告诉我。
PHP:
$rows = $stmt->fetchAll();
if ($rows) {
$response["posts"] = array();
foreach ($rows as $row)
{
$post = array();
$post["username"] = $row["username"];
$post["profile_picture"] = $row["profile_picture"];
array_push($response["posts"], $post);
}
// echoing JSON response
echo json_encode($response);
} else
{
die(json_encode($response));
}
JSON:
{
"posts": [
{
"username": "noah",
"profile_picture": "https://shipstudent.com/animal/appphotos/978321177.jpg"
},
{
"username": "e",
"profile_picture": "https://shipstudent.com/complaint_desk/appphotos/owl.jpeg"
},
]
}
所需格式:
[{
"title": "Dawn of the Planet of the Apes",
"image": "http://api.androidhive.info/json/movies/1.jpg",
"rating": 8.3,
"releaseYear": 2014,
"genre": ["Action", "Drama", "Sci-Fi"]
},
{
"title": "District 9",
"image": "http://api.androidhive.info/json/movies/2.jpg",
"rating": 8,
"releaseYear": 2009,
"genre": ["Action", "Sci-Fi", "Thriller"]
},
【问题讨论】: