我在php中编写一个小服务来输出JSON字符串。该服务最终由Android JSONObject使用,如果这有帮助的话。为了做到简短,我有:
<?php
mysql_connect('localhost','myUser','myPwd') or die('Cannot connect to the DB');
mysql_select_db('ctgscott_myDB') or die('Cannot select the DB');
$query=mysql_query("SELECT name FROM customers ORDER BY name ASC");
while($e=mysql_fetch_assoc($query))
$output[]=$e;
print(json_encode($output));
mysql_close();
&GT;
问题是输出的形式是:
[{"name":"Client Number1"},{"name":"Client Number2"}]
{"name":["Client Number1","Client Number2"]}
非常感谢任何帮助。谢谢!
答案 0 :(得分:1)
您需要拥有一个多维数组。试试这个:
mysql_connect('localhost','myUser','myPwd') or die('Cannot connect to the DB');
mysql_select_db('ctgscott_myDB') or die('Cannot select the DB');
$query=mysql_query("SELECT name FROM customers ORDER BY name ASC");
$names['name'] = array();
while($e = mysql_fetch_assoc($query)) {
$names['name'][] = $e['name'];
}
print(json_encode($names));
答案 1 :(得分:0)
这是一篇很好的文章,使用php http://blog.sptechnolab.com/2011/02/10/android/android-connecting-to-mysql-using-php/从mysql获取数据到android。我希望你喜欢它。