Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php json-JSONarray无法转换为JSONObject_Php_Android_Json - Fatal编程技术网

Php json-JSONarray无法转换为JSONObject

Php 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<

我正在尝试解析从服务器获取的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<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]
));
}