Ayub baba
Ayub baba

Reputation: 81

JSon Parsing About Json Array

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

                }
            });
        }
    }

}

Upvotes: 0

Views: 104

Answers (1)

Nilesh Senta
Nilesh Senta

Reputation: 5472

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.

Upvotes: 1

Related Questions