在我的PHP文件我有代碼包括:爲什麼我的JSON數組在PHP中無法在我的Android應用程序中正確打印?
while ($row = $result->fetch_assoc()) {
//make an array called $results
$results = array();
$results[] = array(
'contact_phonenumber' => $row['username'],
);
$json2 = json_encode($results);
//this is the response which we want in the Android app
echo $json2;
而在我的Android應用程序,我有:
public void onResponse(String response) {
//print the JSON Array from the php file
System.out.println("the jsonarray is : " + response;
的response
當我打印,我得到的格式爲:
[{"contact_phonenumber":"+11111"}][{"contact_phonenumber":"+22222"}][{"contact_phonenumber":"+33333"}][{"contact_phonenumber":"+44444"}][{"contact_phonenumber":"+55555"}]
但不應該是格式? :
[{"contact_phonenumber":"+11111"}, {"contact_phonenumber":"+22222"},{"contact_phonenumber":"+33333"}, {"contact_phonenumber":"+44444"},{"contact_phonenumber":"+55555"}]
我該如何實現這個目標?
你不是創造一個合適的JSON。你正在創建多個json字符串。 –