我有个问题。我现在正尝试数周,以从数据库中获取数据,将其编码为JSON,然后将其发送回我的iOS应用程序。问题是,每次JSON无效时,都会说http://jsonviewer.stack.hu/
这是我现在拥有的代码:

//connection to the database
 $dbhandle = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MySQL");
 //echo "Connected to MySQL<br>";


//select a database to work with
$selected = mysql_select_db("test",$dbhandle)
or die("Could not select examples");

$result = mysql_query("SELECT * FROM test.debiteur WHERE SORT_NAAM LIKE '%eri%'");



while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$deb_nr['deb_nr'] = $row['DEB_NR'];
$deb_naam['name'] = $row['DEB_NAAM'];
$deb_adres['adrs'] = $row['DEB_ADRES'];


$testje = array_merge($deb_nr, $deb_naam, $deb_adres);

$testjevervolg = array('klanten' => array($testje));

 sendResponse(200, json_encode($testjevervolg));
 }
 }


这是它返回的内容:

{
"klanten": [
{
  "deb_nr": "10010",
  "name": "ERIKA Handelsonderneming",
  "adrs": "Aan de Heibloem 17"
}
]
}{
"klanten": [
{
  "deb_nr": "25071",
  "name": "Afdeling Heffing & Invordering",
  "adrs": "Postbus 1275"
}
]
}{
"klanten": [
{
  "deb_nr": "25247",
  "name": "v.d. Heerik b.v.",
  "adrs": "Flemingstraat 3-5"
}
]
}{
"klanten": [
{
  "deb_nr": "25454",
  "name": "Toering Automatisering",
  "adrs": "Appelhof 17a"
}
]
}{
"klanten": [
{
  "deb_nr": "25601",
  "name": "Ratering Bouw & Industrie",
  "adrs": "de Hogenkamp 1"
}
]
}


那就是我得到的。问题是,应该有一个数组“ klanten”
并且应该有每个deb_nr,名称和adrs。现在,每件事都有自己的“ Klanten”
如何解决呢?

谢谢。

最佳答案

创建了这个但尚未测试,目前仅使用textwrangler也许可以帮助您前进。

你可以尝试这样的事情:

$completeJson = array();


while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$deb_nr['deb_nr'] = $row['DEB_NR'];
$deb_nr['name'] = $row['DEB_NAAM'];
$deb_nr['adrs'] = $row['DEB_ADRES'];


 array_push($completeJson,$deb_nr);


 }
 $testjevervolg = array('klanten' => $completeJson);
 sendResponse(200, json_encode($testjevervolg));

08-04 23:46