E/JSON解析器:解析数据时出错[java.lang.Integer类型的值60无法转换为JSONObject] 60(97)
问题描述:
我是android编程的新手。我有一个问题,当我试图从Android中使用JSON从MySQL数据库中获取数据。这是我的方法。这是我收到错误:类型的值60 java.lang.Integer中不能转换到的JSONObjectE/JSON解析器:解析数据时出错[java.lang.Integer类型的值60无法转换为JSONObject] 60(97)
private void getData(final String mac_Address){
//Creating a string request
// Tag used to cancel the request
String tag_string_req = "req_monitor";
StringRequest stringRequest = new StringRequest(Request.Method.POST, AppConfig.URL_MONITOR_HEALTH_PARAMETERS,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//boolean error = j.getBoolean("error");
//j = new JSONObject().append("userid", new JSONArray(json).getInt(0));
//boolean error = j.getBoolean("error");
//return new JSONObject(json.substring(json.indexOf("{"), json.lastIndexOf("}") + 1));
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray("result");
//new JSONObject().append("pulse_rate", new JSONArray(result).getInt(0));
//JSONArray k = null;
for(int i=0;i<result.length();i++){
try {
//Getting json object
JSONObject json = result.getJSONObject(i);
pulse_rate.add(json.getInt("pulse_rate"));
pulse_oxygen_saturation.add(json.getInt("pulse_oxygen_saturation"));
Log.d(TAG, "Fetching pulse rate from mysql: " +pulse_rate+" Fetching pulse rate from mysql: "+pulse_oxygen_saturation);
} catch (JSONException e) {
e.printStackTrace();
}
}
//Calling method getMonitoringData to get the students from the JSON Array
// getMonitoringData(result);
} catch (JSONException e) {
e.printStackTrace();
Log.e("JSON Parser", "Error parsing data [" + e.getMessage()+"] "+response);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
@Override
protected Map<String, String> getParams() {
// Posting parameters to monitor url
Map<String, String> params = new HashMap<String, String>();
params.put("mac_Address", mac_Address);
return params;
}
};
//Creating a request queue
//RequestQueue requestQueue = Volley.newRequestQueue(this);
// Adding request to request queue
AppController.getInstance().addToRequestQueue(stringRequest, tag_string_req);
//Adding request to the queue
//requestQueue.add(stringRequest);
}
这是我的PHP代码
$query = "SELECT pulse_rate, pulse_oxygen_saturation from health_para";
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
$result1 = array();
while ($row = mysqli_fetch_row($result)) {
printf ("%s (%s)\n", $row[0], $row[1]);
array_push($result1,array(
'pulse_rate'=>$row[0],
'pulse_oxygen_saturation'=>$row[1],
));
//$response[]=$row;
//echo json_encode($response);
echo json_encode(array('result'=>$result1));
}
/* free result set */
mysqli_free_result($result);
}
/* close connection */
mysqli_close($link);
} else {
// required post params is missing
$response["error"] = TRUE;
$response["error_msg"] = "Required parameters mac_address is missing!";
echo json_encode($response);
}
这是在我的日志我的JSON响应: 这是我的日志:
{
"result": [{
"pulse_rate": "60",
"pulse_oxygen_saturation": "97"
}, {
"pulse_rate": "60",
"pulse_oxygen_saturation": "97"
}, {
"pulse_rate": "59",
"pulse_oxygen_saturation": "97"
}, {
"pulse_rate": "59",
"pulse_oxygen_saturation": "98"
}, {
"pulse_rate": "58",
"pulse_oxygen_saturation": "98"
}, {
"pulse_rate": "59",
"pulse_oxygen_saturation": "98"
}, {
"pulse_rate": "59",
"pulse_oxygen_saturation": "98"
}, {
"pulse_rate": "58",
"pulse_oxygen_saturation": "98"
}, {
"pulse_rate": "58",
"pulse_oxygen_saturation": "98"
}, {
"pulse_rate": "255",
"pulse_oxygen_saturation": "127"
}, {
"pulse_rate": "70",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "69",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "67",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "66",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "64",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "63",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "64",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "63",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "62",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "63",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "64",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "63",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "62",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "63",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "62",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "61",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "60",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "61",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "63",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "64",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "65",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "64",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "65",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "66",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "67",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "66",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "65",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "64",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "255",
"pulse_oxygen_saturation": "127"
}, {
"pulse_rate": "72",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "73",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "71",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "65",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "62",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "60",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "59",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "60",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "60",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "255",
"pulse_oxygen_saturation": "127"
}, {
"pulse_rate": "66",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "63",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "62",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "61",
"pulse_oxygen_saturation": "99"
}, {
"pulse_rate": "60",
"pulse_oxygen_saturation": "99"
}]
}
答
试试这个。
pulse_rate.add(json.getString("pulse_rate"));
pulse_oxygen_saturation.add(json.getString("pulse_oxygen_saturation"));
+0
我收到此错误:java.lang.NullPointerException pulse_rate.add(json.getInt(“pulse_rate”)); – hermeshabib
请张贴您的JSON响应。你的json响应和解析它的方式似乎有些不同。 –
{“result”:[{“pulse_rate”:“60”,“pulse_oxygen_saturation”:“97”}]} 60(97) {“result”:[{“pulse_rate”:“60”,“pulse_oxygen_saturation” “97”},{“pulse_rate”:“60”,“pulse_oxygen_saturation”:“97”}]} 59(97) – hermeshabib
它看起来不像一个有效的JSON。请发布正确和有效的JSON响应。 –