我正在尝试将json数组传递给我的android工作室,所以我可以显示从mysql服务器检索数据但是我一直收到这个错误:
W / System.err:org.json.JSONException:Value [{" ID":" 36"" REPORTTYPE":"犯罪""地址":&#34 ;共和国 新月","状态":"等待"},]类型org.json.JSONArray不能 转换为JSONObject
任何善良的灵魂都可以帮助我解决这个问题吗?
PHP代码:
$input = file_get_contents("php://input");
$jsonObj = json_decode($input, true);
$userid = $jsonObj['userId'];
$query_rsUser = sprintf("SELECT * FROM reports WHERE userid = %s",$userid);
$rsUser = mysqli_query($connDB, $query_rsUser);
while($row_rsUser = mysqli_fetch_assoc($rsUser)) {
$results[]=$row_rsUser;
}
echo json_encode($results);
mysqli_close($connDB);
?>
Java代码:
try {
JSONArray resultArray = jsonObject.getJSONArray("reportId");
reports.clear();
// Store all results into locations table layout
for (int i = 0; i < resultArray.length(); i++) {
JSONObject resultObj = resultArray.getJSONObject(i);
String reportid = resultObj.getString("id");
String reportType = resultObj.getString("reportType");
String address = resultObj.getString("address");
String status = resultObj.getString("status");
JSONObject ReportObj = resultObj.getJSONObject("resultObj");
reports.add(new ReportObj(reportid, reportType, address, status));
}
} catch (Exception e) {
e.printStackTrace();
}