donglu1971
2017-05-10 07:00
浏览 111

json- JSONarray无法转换为JSONObject

I am trying to parse the JSON response get from the server and I am getting the error message: JSONArray cannot be converted to 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);
           }

this is my php code`

 <?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 this is my response string, i can't type here because it's too long

how to solve this problems? i tried many methods but can't find any solution.

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条回答 默认 最新

  • douba8819 2017-05-10 07:04
    已采纳

    Change the line

    JSONObject json_data = jArray.getJSONObject(i);
    

    To

    JSONArray innerArray = jArray.getJSONArray(i);
    

    and loop through the JSONArray again.

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

    Your outer JSONArray contains JSONArrays and not JSONObjects which can be seen in your PHP code

    $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]
    ));
    }
    
    已采纳该答案
    打赏 评论