“ JSONException:类型为java.lang.String的值<br不能转换为JSONObject”?

时间:2019-11-13 02:57:22

标签: php android mysql json jsonobjectrequest

我在具有PHP和MySQL的Android中使用JsonObjectRequest并得到以下错误消息:

"JSONException: Value <br of type java.lang.String cannot be converted to JSONObject"

下面是我的PHP代码:

<?php

$month = $_POST['month'];

$connection = new mysqli("localhost","root","","Fubon");
$dateCheckSQLCommand = "select * from clockindata where Month(date)= $month  ";

if ($result = $connection->query($dateCheckSQLCommand)) {

    while ($row = $result->fetch_assoc()) {
        echo json_encode($row);
    }

  $result->free();

}

$connection->close();
?>

有人说br是html中的换行符,它不是Json格式吗?

此外,我在MySQL“日期”列中使用DateTime类型或Timestamp类型。我对PHP的Json响应如下所示。是关于MySQL设置还是PHP问题? :

{"account":"Fu","ssid":"Fu","date":"2019-11-14 00:00:00"},{"account":"Fu","ssid":"Fu","date":"2019-11-21 00:00:00"}

2 个答案:

答案 0 :(得分:0)

不要转换以json编码的每一行,而是将所有行添加到数组中并将数组转换为json,就像下面的

<?php

$month = $_POST['month'];

$connection = new mysqli("localhost","root","","Fubon");
$dateCheckSQLCommand = "select * from clockindata where Month(date)= $month  ";
$data = [];
if ($result = $connection->query($dateCheckSQLCommand)) {

    while ($row = $result->fetch_assoc()) {
        $data[] = $row;
    }

  $result->free();

}
echo json_encode($data);
$connection->close();
?>

答案 1 :(得分:0)

我发现了我的错误。我Jsonrequest从Android向PHP发送数据,但是我的JsonRequest设置是GET而不是POST,因此它返回错误消息“ PHP没有得到值”而不是JsonArray。谢谢你的帮助 !我真的很感激!