我试图以这样的格式输出我的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"]
},
答案 0 :(得分:3)
$rows = $stmt->fetchAll();
$response = [];
foreach ($rows as $row) {
$post = [
"username" => $row["username"],
"profile_picture" => $row["profile_picture"]
];
$response[] = $post;
}
echo json_encode($response);
答案 1 :(得分:0)
$response = array();
foreach ($rows as $row)
{
$post = array();
$post["title"] => // define title;
$post["image"] => // define image;
$post["rating"] => // define rating;
$post["releaseYear"] => // define releaseYear;
$post["genre"] => // define genre, must be array();
array_push($response, $post);
}