2015-11-06 41 views
-1

我要創建一個用於從數據庫接收數據的android應用程序。我測試了我的應用程序,它只能接收特定的JSON格式。我嘗試添加此JSON頭PHP的文檔中,在php上爲android創建JSON標頭

報頭( '內容類型:應用程序/ JSON;字符集= UTF-8');

然而,它是無用的。

,這是我的JSON印刷由PHP:

[ 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"s\",\"password\":\"s\"}", 
"{\"user\":\"hi\",\"password\":\"tom\"}", 
"{\"user\":\"vhj\",\"password\":\"fgg\"}", 
"{\"user\":\"d\",\"password\":\"h\"}", 
"{\"user\":\"dh\",\"password\":\"hl\"}", 
"{\"user\":\"p\",\"password\":\"p\"}", 
"{\"user\":\"pon\",\"password\":\"pon\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"s\",\"password\":\"s\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}", 
"{\"user\":\"\",\"password\":\"\"}" 
] 

,我想改變它格式化這個

{ 
"goodstart":[ 
{ 
    "user": "A", 
    "password": "123" 
} 
] 
} 

尋求幫助!

PHP代碼

<?php 
header('Content-Type: application/json'); 

$con=mysqli_connect(HOST,USER,PASSWORD); 

$dbconnect=mysqli_select_db($con, DB) or die ('Select Error:'.mysql_error()); 

$sql_query="SELECT * FROM `goodstart`"; 

$result=mysqli_query($con, $sql_query); 

$output=array(); 

while($row=$result->fetch_assoc()){ 
    $output[]= json_encode($row); 
} 
    print json_encode($output, JSON_PRETTY_PRINT); 

mysql_close($con); 
?> 
+1

顯示PHP代碼中創建的JSONObject –

+0

這是我的PHP代碼 –

+0

這個問題不清楚。你在問如何編寫JSON或者你遇到特殊問題? – Sufian

回答

0
echo "{\n"; 
    echo '"goodstart":'; 
    print str_replace('",', "\",\n",str_replace('}",', "\n},",str_replace('"{', "{\n",str_replace('\"', '"',json_encode($output, JSON_PRETTY_PRINT))))); 
    echo "\n}"; 

解決