我已成功将一些数据上传到名为posts的表中。现在我想生成一些JSON,用于Android开发目的。 所以这是我获取JSON的代码。
<?php
include("../functions/connect.php");
$string = "";
$posts_array = array();
$get_posts = "select * from posts";
$run_posts = mysqli_query($con,$get_posts);
while($posts_row = mysqli_fetch_array($run_posts)){
$row_array['post_id'] = $posts_row['post_id'];
$row_array['category_id'] = $posts_row['category_id'];
$row_array['post_title'] = $posts_row['post_title'];
$row_array['post_date'] = $posts_row['post_date'];
$row_array['post_author'] = $posts_row['post_author'];
$row_array['post_keywords'] = $posts_row['post_keywords'];
$row_array['post_image'] = $posts_row['post_image'];
$row_array['post_content'] = $posts_row['post_content'];
array_push($posts_array,$row_array);
$string = json_encode($posts_array);
echo $string;
}
?>
我的JSON虽然如下所示。
[
{
"post_id":"6",
"category_id":"1",
"post_title":"Turkey challenges Russia over IS oil trade claim",
"post_date":"12-01-15",
"post_author":"BBC",
"post_keywords":"bbc,war",
"post_image":"post1.jpg",
"post_content":"Turkey has challenged Russia to prove its claim that Ankara shot down a Russian plane in order to protect its oil trade with Islamic State.\nIf you allege something you should prove it, Turkish President Recep Tayyip Erdogan said."
}
][
{
"post_id":"6",
"category_id":"1",
"post_title":"Turkey challenges Russia over IS oil trade claim",
"post_date":"12-01-15",
"post_author":"BBC",
"post_keywords":"bbc,war",
"post_image":"post1.jpg",
"post_content":"Turkey has challenged Russia to prove its claim that Ankara shot down a Russian plane in order to protect its oil trade with Islamic State.\nIf you allege something you should prove it, Turkish President Recep Tayyip Erdogan said."
},
{
"post_id":"8",
"category_id":"1",
"post_title":"Faulty part caused AirAsia crash",
"post_date":"12-01-15",
"post_author":"BBC",
"post_keywords":"aircrash,AirAsia",
"post_image":"breaking_news.png",
"post_content":"A faulty component was a major factor when an AirAsia plane crashed into the Java Sea, killing 162 people last December, Indonesian officials say.\nThe first major report into the crash found that actions by the crew in response to the malfunction also contributed to the disaster.\nThe Airbus A320-200, going from Surabaya to Singapore, was 40 minutes into the flight when contact was lost.\nThe report is the result of a year-long investigation."
}
]
我希望它是这样的。
[
{
"post_id":"6",
"category_id":"1",
"post_title":"Turkey challenges Russia over IS oil trade claim",
"post_date":"12-01-15",
"post_author":"BBC",
"post_keywords":"bbc,war",
"post_image":"post1.jpg",
"post_content":"Turkey has challenged Russia to prove its claim that Ankara shot down a Russian plane in order to protect its oil trade with Islamic State.\nIf you allege something you should prove it, Turkish President Recep Tayyip Erdogan said."
},
{
"post_id":"8",
"category_id":"1",
"post_title":"Faulty part caused AirAsia crash",
"post_date":"12-01-15",
"post_author":"BBC",
"post_keywords":"aircrash,AirAsia",
"post_image":"breaking_news.png",
"post_content":"A faulty component was a major factor when an AirAsia plane crashed into the Java Sea, killing 162 people last December, Indonesian officials say.\nThe first major report into the crash found that actions by the crew in response to the malfunction also contributed to the disaster.\nThe Airbus A320-200, going from Surabaya to Singapore, was 40 minutes into the flight when contact was lost.\nThe report is the result of a year-long investigation."
}
]
我想这是由我的PHP代码引起的错误。有什么想法吗?
感谢。
答案 0 :(得分:2)
尝试移动while循环
$string = json_encode($posts_array);
echo $string;
所以我们走了
<?php
include("../functions/connect.php");
$string = "";
$posts_array = array();
$get_posts = "select * from posts";
$run_posts = mysqli_query($con,$get_posts);
while($posts_row = mysqli_fetch_array($run_posts)){
$row_array['post_id'] = $posts_row['post_id'];
$row_array['category_id'] = $posts_row['category_id'];
$row_array['post_title'] = $posts_row['post_title'];
$row_array['post_date'] = $posts_row['post_date'];
$row_array['post_author'] = $posts_row['post_author'];
$row_array['post_keywords'] = $posts_row['post_keywords'];
$row_array['post_image'] = $posts_row['post_image'];
$row_array['post_content'] = $posts_row['post_content'];
array_push($posts_array,$row_array);
}
$string = json_encode($posts_array);
echo $string;
?>
答案 1 :(得分:1)
第二个while循环后,$ string变量不为空。只需在while循环后回显。