我需要输出一个JSON对象供iphone使用
我能输出像
{"feed":{"id":"1","player":"player1"}}
{"feed":{"id":"1","player":"player2"}}
{"feed":{"id":"2","player":"player3"}}
代码:
$query = "SELECT id,player FROM MyVideos";
$result = mysql_query($query,$link) or die('Errant query: '.$query);
$players[] = array();
if(mysql_num_rows($result)){
while($player = mysql_fetch_assoc($result)){
$players[] = array('player'=>$player);
echo json_encode(array("feed"=>$player));
}
}
但是我需要输出像这样的东西
{"feed":
{"id":"1","player":"player1"},
{"id":"1","player":"player2"},
{"id":"2","player":"player3"}
}
任何人都可以帮助我。
谢谢,
答案 0 :(得分:7)
您发布的输出无效JSON,您需要在feed
中的项目周围添加括号:
{"feed": [
{"id":"1","player":"player1"},
{"id":"1","player":"player2"},
{"id":"2","player":"player3"}
]}
您应该循环搜索结果并构建一个Feed项目数组,然后立即输出所有内容,如下所示:
$feed_items = array();
if (mysql_num_rows($result)) {
while ($player = mysql_fetch_assoc($result)){
$feed_items[] = $player;
}
}
echo json_encode(array("feed" => $feed_items));