json- JSONarray无法转换为JSONObject

时间:2017-05-10 07:00:13

标签: 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<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);
           }

这是我的PHP代码

 <?php
require "connection.php";
$sql = "select * from main";

$res = mysqli_query($con,$sql);

$result = array();

while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'name'=>$row[1],
'harga'=>$row[2],
'gambar'=>$row[3]
));
}

echo json_encode(array($result));

mysqli_close($con);

?>

这是我的回复字符串this is my response string, i can't type here because it's too long

如何解决这个问题?我尝试了很多方法,但找不到任何解决方案。

error Logcat

    05-10 03:47:08.600 8806-8806/foodsqlitedemo.quocnguyen.com.foodsqlitedemo E/RecyclerView: No adapter attached; skipping layout
05-10 03:47:08.900 8806-8806/foodsqlitedemo.quocnguyen.com.foodsqlitedemo E/RecyclerView: No adapter attached; skipping layout
05-10 03:47:09.220 8806-8806/foodsqlitedemo.quocnguyen.com.foodsqlitedemo W/ResourceType: No package identifier when getting value for resource number 0x00000001
05-10 03:47:09.220 8806-8806/foodsqlitedemo.quocnguyen.com.foodsqlitedemo D/AndroidRuntime: Shutting down VM
05-10 03:47:09.220 8806-8806/foodsqlitedemo.quocnguyen.com.foodsqlitedemo W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x9cd5db20)
05-10 03:47:09.220 8806-8806/foodsqlitedemo.quocnguyen.com.foodsqlitedemo E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                            Process: foodsqlitedemo.quocnguyen.com.foodsqlitedemo, PID: 8806
                                                                                            android.content.res.Resources$NotFoundException: String resource ID #0x1
                                                                                                at android.content.res.Resources.getText(Resources.java:244)
                                                                                                at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
                                                                                                at android.widget.TextView.setText(TextView.java:3888)
                                                                                                at com.kelompok3.restaurantapp.restoranfixx.Adapter.AdapterFood.onBindViewHolder(AdapterFood.java:50)
                                                                                                at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6067)
                                                                                                at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6100)
                                                                                                at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5282)
                                                                                                at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5158)
                                                                                                at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2061)
                                                                                                at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1445)
                                                                                                at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1408)
                                                                                                at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:580)
                                                                                                at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3379)
                                                                                                at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3188)
                                                                                                at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3632)
                                                                                                at android.view.View.layout(View.java:14817)
                                                                                                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                                                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                                                                                                at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1660)
                                                                                                at android.widget.LinearLayout.onLayout(LinearLayout.java:1436)
                                                                                                at android.view.View.layout(View.java:14817)
                                                                                                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                                                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                                                at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                                                at android.view.View.layout(View.java:14817)
                                                                                                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                                                at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
                                                                                                at android.view.View.layout(View.java:14817)
                                                                                                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                                                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                                                at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                                                at android.view.View.layout(View.java:14817)
                                                                                                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                                                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                                                                                                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
                                                                                                at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
                                                                                                at android.view.View.layout(View.java:14817)
                                                                                                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                                                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                                                at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                                                at android.view.View.layout(View.java:14817)
                                                                                                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                                                                at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
                                                                                                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
                                                                                                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
                                                                                                at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
                                                                                                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                                                                                                at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                                                                                                at android.view.Choreographer.doFrame(Choreographer.java:544)
                                                                                                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                                                                                                at android.os.Handler.handleCallback(Handler.java:733)
                                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                at android.os.Looper.loop(Looper.java:136)
                                                                                                at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                                                at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                                at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                                                at com.android.i

1 个答案:

答案 0 :(得分:0)

更改行

JSONObject json_data = jArray.getJSONObject(i);

JSONArray innerArray = jArray.getJSONArray(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);
}

您的外部JSONArray包含JSONArrays,而不是可以在PHP代码中看到的JSONObjects

$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]
));
}