0

This is error :Value [] at 0 of type org.json.JSONArray cannot be converted to JSONObject I had tried to pass parameter using post method but its giving the above error JSON I am Trying is `

[
    {
        "menu_items": [
            {
                "menu_name": "Beverages",
                "items": [
                    {
                        "id": 1,
                        "BaseName": "Coca-Cola",
                        "itemdesc": "",
                        "subitems": [
                            {
                                "id": 1,
                                "SubItemdesc": "0.33L",
                                "SubItemprice": "0.90"
                            },
                            {
                                "id": 2,
                                "SubItemdesc": "1.5L",
                                "SubItemprice": "2.00"
                            }
                        ]
                    },
                    {
                        "id": 2,
                        "BaseName": "Diet Coca-Cola",
                        "itemdesc": "",
                        "subitems": [
                            {
                                "id": 1,
                                "SubItemdesc": "0.33L",
                                "SubItemprice": "0.90"
                            },
                            {
                                "id": 2,
                                "SubItemdesc": "1.5L",
                                "SubItemprice": "2.00"
                            }
                        ]
                    },
                    {
                        "id": 3,
                        "BaseName": "Fanta",
                        "itemdesc": "",
                        "subitems": [
                            {
                                "id": 1,
                                "SubItemdesc": "0.33L",
                                "SubItemprice": "0.90"
                            }
                        ]
                    },
                    {
                        "id": 4,
                        "BaseName": "Tango",
                        "itemdesc": "",
                        "subitems": [
                            {
                                "id": 1,
                                "SubItemdesc": "0.33L",
                                "SubItemprice": "0.90"
                            }
                        ]
                    }
                ]
            }'

This is code I am writing not able to understand y is it causing like that

public class Secondlevel extends Activity {

    ArrayList<JSONParser> itemsdata;
    String item;
    Block b=new Block();
    Activity activity;
    ListView sec;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondlist);
        sec= (ListView) findViewById(R.id.seondlst);
        Intent gettext=getIntent();
        item=gettext.getStringExtra("name");
        TextView test= (TextView) findViewById(R.id.textView);
        test.setText(item);
        itemsdata=new ArrayList<JSONParser>();
        new loaditems().execute();
    }
    public class loaditems extends AsyncTask<String,String,String>{

        @Override
        protected String doInBackground(String... strings) {

            String url="http://www.yell4food.com/json/data_standard_item_new.php?rname=standardtakeaway";
            List<NameValuePair> params=new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("cname", item));
            String plz=b.makeHttpRequest(url,"GET",params);
            try {
                JSONArray items=new JSONArray(plz);
                for(int i=0;i<items.length();i++){
                    JSONObject citems=items.getJSONObject(i);
                    JSONArray menu_items=citems.getJSONArray("menu_items");
                    for (int j=0;j<menu_items.length();j++){
                        JSONObject sitems=menu_items.getJSONObject(j);
                        sitems.getString("menu_name");
                        JSONArray titems=sitems.getJSONArray("items");
                        for (int k=0;k<titems.length();k++){
                            JSONObject fitems=titems.getJSONObject(k);
                            JSONParser jsonParser=new JSONParser();
                            jsonParser.setBaseName(fitems.getString("BaseName"));
                            jsonParser.setItemdesc(fitems.getString("itemdesc"));
                            JSONArray fivitem=fitems.getJSONArray("subitems");
                            for(int l=0;l<fivitem.length();l++){
                                JSONObject subitems=fivitem.getJSONObject(l);
                                jsonParser.setSubItemdesc(subitems.getString("SubItemdesc"));
                                jsonParser.setSubItemprice(subitems.getString("SubItemprice"));
                                itemsdata.add(jsonParser);
                            }


                        }

                    }


                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Second_adapter secondAdapter=new Second_adapter(activity,itemsdata);
                    sec.setAdapter(secondAdapter);

                }
            });
        }
    }

}
4
  • 2
    You are converting JsonArray to JsonObject, that is why the error JSONArray cannot be converted to JSONObject Commented Oct 27, 2015 at 4:41
  • i think i am not doing that.. Commented Oct 27, 2015 at 4:51
  • JSONObject citems=items.getJSONObject(i); it should be JSONArray, because as per your json items is an array Commented Oct 27, 2015 at 4:54
  • i havnt understood it bro Commented Oct 27, 2015 at 5:09

1 Answer 1

1

There is a better solution for parsing JSON.

Add dependency in build.xml (For Android Studio) or Download Jar file http://www.java2s.com/Code/Jar/g/Downloadgson222jar.htm

dependencies { compile 'com.google.code.gson:gson:2.2.4' }

Create your Model Class

import java.util.List;
public class ModelObject {

    private List<MenuItemsEntity> menu_items;

    public void setMenu_items(List<MenuItemsEntity> menu_items) {
        this.menu_items = menu_items;
    }

    public List<MenuItemsEntity> getMenu_items() {
        return menu_items;
    }

    public static class MenuItemsEntity {
        private String menu_name;

        private List<ItemsEntity> items;

        public void setMenu_name(String menu_name) {
            this.menu_name = menu_name;
        }

        public void setItems(List<ItemsEntity> items) {
            this.items = items;
        }

        public String getMenu_name() {
            return menu_name;
        }

        public List<ItemsEntity> getItems() {
            return items;
        }

        public static class ItemsEntity {
            private int id;
            private String BaseName;
            private String itemdesc;
            private List<SubitemsEntity> subitems;

            public void setId(int id) {
                this.id = id;
            }

            public void setBaseName(String BaseName) {
                this.BaseName = BaseName;
            }

            public void setItemdesc(String itemdesc) {
                this.itemdesc = itemdesc;
            }

            public void setSubitems(List<SubitemsEntity> subitems) {
                this.subitems = subitems;
            }

            public int getId() {
                return id;
            }

            public String getBaseName() {
                return BaseName;
            }

            public String getItemdesc() {
                return itemdesc;
            }

            public List<SubitemsEntity> getSubitems() {
                return subitems;
            }

            public static class SubitemsEntity {
                private int id;
                private String SubItemdesc;
                private String SubItemprice;

                public void setId(int id) {
                    this.id = id;
                }

                public void setSubItemdesc(String SubItemdesc) {
                    this.SubItemdesc = SubItemdesc;
                }

                public void setSubItemprice(String SubItemprice) {
                    this.SubItemprice = SubItemprice;
                }

                public int getId() {
                    return id;
                }

                public String getSubItemdesc() {
                    return SubItemdesc;
                }

                public String getSubItemprice() {
                    return SubItemprice;
                }
            }
        }
    }
}

For Parsing

ModelObject model = new Gson().fromJson("Your JSON String", ModelObject.class);

I hope you like it.

Sign up to request clarification or add additional context in comments.

4 Comments

i had never tried gson bro i have to work on it i am newbie for android i will try it for sure
Ok, Your JSON String startwith Array So try below code ModelObject[] obj = new Gson().fromJson(Json, ModelObject[].class); Log.e("Log",""+obj[0].getMenu_items().get(0).getMenu_name());
dropbox.com/s/pw27g0p38ib6uea/JsonParser.zip?dl=0 Just Download and Run It See Logcat Output. :)
its working like charm bro love u can i set that in the list view?yell4food.com/json/… this the url i am working on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.