我将创建一个用于从数据库接收数据的android应用程序。我测试了我的应用程序,它只能接收特定的JSON格式。我尝试在php文档中添加这个JSON头
header('Content-Type:application/json;charset=utf-8');
然而,它是无用的。
这是我用php:打印的JSON
[
"{'"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);
?>
echo "{'n";
echo '"goodstart":';
print str_replace('",', "'",'n",str_replace('}",', "'n},",str_replace('"{', "{'n",str_replace(''"', '"',json_encode($output, JSON_PRETTY_PRINT)))));
echo "'n}";
已解决