Php json-JSONarray无法转换为JSONObject
我正在尝试解析从服务器获取的JSON响应,并收到错误消息:JSONArray无法转换为JSONObjectPhp json-JSONarray无法转换为JSONObject,php,android,json,Php,Android,Json,我正在尝试解析从服务器获取的JSON响应,并收到错误消息:JSONArray无法转换为JSONObject org.json_JSONEXCEPTION: Value [{}] at 0 of type org.json.JSONArray cannot be converted to JSONObject try { JSONArray jArray = new JSONArray(result); for(int i=0;i<
org.json_JSONEXCEPTION: Value [{}] at 0 of type org.json.JSONArray cannot be converted to JSONObject
try {
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
DataFood foodData = new DataFood();
foodData.foodImage= json_data.getString("gambar");
foodData.foodName= json_data.getString("nama");
foodData.foodId= json_data.getInt("id");
foodData.price= json_data.getInt("harga");
data.add(foodData);
}
换行JSONObject json_data = jArray.getJSONObject(i);
到
然后再次循环通过JSONArray
for (int i = 0; i < innerArray.length(); i++) {
JSONObject json_data = innerArray.getJSONObject(i);
DataFood foodData = new DataFood();
foodData.foodImage= json_data.getString("gambar");
foodData.foodName= json_data.getString("name");
foodData.foodId= json_data.getInt("id");
foodData.price= json_data.getInt("harga");
data.add(foodData);
}
for(int i=0;i
外部JSONArray包含JSONArray,而不是可以在PHP代码中看到的JSONObject
$result = array(); <-- your main array
while($row = mysqli_fetch_array($res)){
array_push($result, <-- you add a new array to your main array
array('id'=>$row[0],
'name'=>$row[1],
'harga'=>$row[2],
'gambar'=>$row[3]
));
}
$result=array()$第[1]行,
“harga'=>美元行[2],
“gambar”=>美元行[3]
));
}
您的结果字符串是什么?请提供您的响应字符串。我必须更改此行吗?JSONArray jArray=新JSONArray(结果);不,这行代码仍然可以遵循您的建议,但它说“无法解析方法大小()”对不起,先生,我是新手。如果问题正在减少,响应字符串是“org.JSONException No value for nama”,应该是name
,我刚刚复制了您的JSON
JSONArray innerArray = jArray.getJSONArray(i);
for (int i = 0; i < innerArray.length(); i++) {
JSONObject json_data = innerArray.getJSONObject(i);
DataFood foodData = new DataFood();
foodData.foodImage= json_data.getString("gambar");
foodData.foodName= json_data.getString("name");
foodData.foodId= json_data.getInt("id");
foodData.price= json_data.getInt("harga");
data.add(foodData);
}
$result = array(); <-- your main array
while($row = mysqli_fetch_array($res)){
array_push($result, <-- you add a new array to your main array
array('id'=>$row[0],
'name'=>$row[1],
'harga'=>$row[2],
'gambar'=>$row[3]
));
}