how to retrieve json nested array datas and populate into listview?

  • Get Json Nested array datas into Arraylist.
  • Populate it into List View
  • Had Spinner also, whenever spinner data changes List view datas also changes..
  • Is this possible to do in android?

    {
      "Topping": [
        {
          "topping_id": 1,
          "topping_title": "title_1",
          "topping_price": 1,
          "Foods": [
            {
              "price": 1,
              "lable": "price_1"
            },
            {
              "price": 1.5,
              "lable": "price_2"
            },
            {
              "price": 2,
              "lable": "price_3"
            }
          ]
        },
        {
          "topping_id": 2,
          "topping_title": "title_2",
          "topping_price": 1,
          "Foods": [
            {
              "price": 1,
              "lable": "price_1"
            },
            {
              "price": 1.5,
              "lable": "price_2"
            },
            {
              "price": 2,
              "lable": "price_3"
            }
          ]
        },
        {
          "topping_id": 3,
          "topping_title": "title_3",
          "topping_price": 1,
          "Foods": [
            {
              "price": 1,
              "lable": "price_1"
            },
            {
              "price": 1.5,
              "lable": "price_2"
            },
            {
              "price": 2,
              "lable": "price_3"
            }
          ]
        }
      ],
      "Food_sizes": [
        {
          "price": 10,
          "lable": "10" SML",
          "size": "price_1"
        },
        {
          "price": 12,
          "lable": "14" MED",
          "size": "price_2"
        },
        {
          "price": 14,
          "lable": "16" LRG",
          "size": "price_3"
        }
      ]
    }
    

    This is my json format and Food_Sizes array i want to display into Spinner(Single Select)... Food price and its name want to display into listview... whenever spinner value changes price of list view value also changes... My Code for getting this data...

     stringtoppingcombo = new ArrayList<>();
        string_spinner_size = new ArrayList<>();
            try {
                List<NameValuePair> request_topping = new ArrayList<NameValuePair>();
                request_topping.add(new BasicNameValuePair("food_id", title_id));
                JSONObject jobject = jparser.makeHttpRequest(GET_TOPPING, "POST", request_topping);
                jarray1 = jobject.getJSONArray("Topping");
                    for(int i=0; i<jarray1.length();i++) {
                    JSONObject jobjet = jarray1.getJSONObject(i);
                        ToppingCombo item = new ToppingCombo(); 
                            item.setTOPPINGID(jobjet.getInt("topping_id"));
                            item.setTOPPINGTITLE(jobjet.getString("topping_title"));
                            item.setTOPPINGPRICE(Float.parseFloat(jobjet.getString("topping_price")));
                                JSONArray food_array = jobjet.getJSONArray("Foods");
                                  for (int k=0; k < food_array.length(); k++) {
                                      JSONObject food_object = food_array.getJSONObject(k);
                                            Food_Topping_Items_Sizes items_size = new Food_Topping_Items_Sizes();
                                            items_size.setPriceTopping(Float.parseFloat(food_object.getString("price")));
                                            items_size.setLabelTopping(food_object.getString("lable"));
                                                item.setFoodItems(items_size);
                                        }
                                  toppingcomboitem.add(item);
                                }
                            jsonarray= jobject.getJSONArray("Food_sizes");
                            for(int j=0; j < jsonarray.length(); j++) {
                                JSONObject jsonobject_food_size = jsonarray.getJSONObject(j);
                                Food_size_Items items = new Food_size_Items();
                                items.setLabel(jsonobject_food_size.getString("lable"));
                                items.setPrice(Float.parseFloat(jsonobject_food_size.getString("price")));
                                items.setSize(jsonobject_food_size.getString("size"));
                                mFoodSize.add(items);
                                string_spinner_size.add(jsonobject_food_size.getString("lable").concat(" ").concat("- $").
                                        concat(jsonobject_food_size.getString("price")));
                                            }
    

    I displayed in listview like below..

    public View getView( final int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        if(null == rowView) {
                rowView = mLayoutInflater.inflate(R.layout.list_toppingcheckbox, null);
                viewHolder = new ViewHolder();
                viewHolder.check_data = (CheckBox) rowView.findViewById(R.id.check_datas);
                viewHolder.check_text = (TextView) rowView.findViewById(R.id.check_text);
                rowView.setTag(viewHolder);
                        }
            else {
                viewHolder = (ViewHolder) rowView.getTag();
                    }
            final ViewHolder viewHolder = (ViewHolder)rowView.getTag();
            viewHolder.check_data.setText(toppingcomboitem.get(position).getTOPPINGTITLE().concat(" $ "));
            spinner_selected_item_position_string = spinner_selected_item_position_text.getText().toString();
            spinner_selected_item_position_int = Integer.parseInt(spinner_selected_item_position_string);
            viewHolder.check_text.setText(String.valueOf(toppingcomboitem.get(position).getFoodItems().getPriceTopping()));
    

    My spinner displays like this...

    if(string_spinner_size != null && string_spinner_size.size() > 1) { spinner_size.setVisibility(View.VISIBLE); spinner_size.setAdapter(new ArrayAdapter(ToppingItems.this, android.R.layout.simple_spinner_dropdown_item, string_spinner_size)); spinner_selected_item_position_text.setText(String.valueOf(spinner_selected_item_position)); spinner_size.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView arg0, View arg1, int position, long arg3) { topping_spinner_price = new TextView(ToppingItems.this); topping_spinner_price.setText(String.valueOf(mFoodSize.get(position).getPrice())); topping_string_price = topping_spinner_price.getText().toString();

                        topping_spinner_title = new TextView(ToppingItems.this);
                        topping_spinner_title.setText(mFoodSize.get(position).getLabel());
                        topping_string_title = topping_spinner_title.getText().toString();
    
                        topping_spinner_size = new TextView(ToppingItems.this);
                        topping_spinner_size.setText(mFoodSize.get(position).getSize());
                        topping_string_size = topping_spinner_size.getText().toString();
    
                        spinner_selected_item_position = spinner_size.getSelectedItemPosition();
                        spinner_selected_item_position_text.setText(String.valueOf(spinner_selected_item_position));
                        topping_checkbox_adapter.notifyDataSetChanged();
    
                        }
                        @Override
                        public void onNothingSelected(AdapterView<?> arg0) { }                          
                        });
                        }  else { 
                        spinner_size.setVisibility(View.INVISIBLE); 
                        }
    

    You can use nest for loop to get nest string like following,

         JSONObject  jsonRootObject = new JSONObject(jsonString); 
    
         //Get the instance of JSONArray that contains JSONObjects
         JSONArray jsonArray = jsonRootObject.getJSONArray("Employee");
    
         //Iterate the jsonArray to get values 
         for(int i=0; i < jsonArray.length(); i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
    
         int id = Integer.parseInt(jsonObject.getString("id"));
    
              //assume name contains first name and last name (nest strings)
              JSONArray jsonArray1 = jsonRootObject.getJSONArray("name");
    
             //Iterate the jsonArray1 to get nest values 
             for(int inner=0;inner < jsonArray1.length();inner++){
    
                 JSONObject jsonObject1 = jsonArray1.getJSONObject(inner);  
                 String firstname=jsonObject1.getString("firstname");
                 String lastname=jsonObject1.getString("lastname"); 
             }  
        }
    

    Android nested JSON Parsing Tutorial


    this is really 3 different questions but 1. You should use JSONObject which gives you all of the features you need for parsing json :

    JSONObject object = new JSONObject(YOUR_STRING);
    

    now you can do whatever you want with this object like :

    object.getString("dsf");
    object.getJSONObject("nested");
    

    Yes.. Finally i found the solution...

     jarray1 = jobject.getJSONArray("Topping");
                        for(int i=0; i<jarray1.length();i++) {
                        JSONObject jobjet = jarray1.getJSONObject(i);
                                    ToppingCombo item = new ToppingCombo(); 
                                    item.setTOPPINGID(jobjet.getInt("topping_id"));
                                    item.setTOPPINGTITLE(jobjet.getString("topping_title"));
                                    item.setTOPPINGPRICE(Float.parseFloat(jobjet.getString("topping_price")));
                      JSONArray food_array = jobjet.getJSONArray("Foods");
                      ArrayList<Food_Topping_Items_Sizes> food_topping_sizes = new ArrayList<Food_Topping_Items_Sizes>();
                         for (int k=0; k < food_array.length(); k++) {
                                    JSONObject food_object = food_array.getJSONObject(k);
                                    Food_Topping_Items_Sizes itemsize = new Food_Topping_Items_Sizes();
                                        price = food_object.getString("price");
                                        itemsize.setPriceTopping(Float.parseFloat(price));
                                        lable = food_object.getString("lable");
                                        itemsize.setLabelTopping(lable);
                                        food_topping_sizes.add(itemsize);
                                                    }
                                        item.setFoodItems(food_topping_sizes);
                                        toppingcomboitem.add(item);
                                            }
                            jsonarray= jobject.getJSONArray("Food_sizes");
                            for(int j=0; j < jsonarray.length(); j++) {
                                JSONObject jsonobject_food_size = jsonarray.getJSONObject(j);
                                Food_size_Items items = new Food_size_Items();
                                items.setLabel(jsonobject_food_size.getString("lable"));
                                food_lable_checking = jsonobject_food_size.getString("lable");
                                items.setPrice(Float.parseFloat(jsonobject_food_size.getString("price")));
                                items.setSize(jsonobject_food_size.getString("size"));
                                mFoodSize.add(items);
                                string_spinner_size.add(jsonobject_food_size.getString("lable").concat(" ").concat("- $").
                                        concat(jsonobject_food_size.getString("price")));
                                            }
                                        }
    
    
    
    
    
    And my Model Class is...
    
    
    
    
    public class ToppingCombo implements Parcelable {
    private int topping_id;
    private String topping_title;
    private float topping_price;
    private String topping_food_size_name;
    private float topping_food_size_price;
    private boolean selected;
    private ArrayList<Food_Topping_Items_Sizes> food_topping_sizes;
    private String label_topping;
    private float price_topping;
    
    public boolean isSelected() {
        return selected;
            }
    public void setSelected(boolean selected) {
        this.selected = selected;
            }
    
    public ArrayList<Food_Topping_Items_Sizes> getFoodItems() {
        return food_topping_sizes;
            }
    public void setFoodItems(ArrayList<Food_Topping_Items_Sizes> food_topping_sizes2) {
        this.food_topping_sizes = food_topping_sizes2;
    }
    
    public float getPriceTopping() {
        return price_topping;
            }
    public void setPriceTopping(float food_price) {
        price_topping = food_price;
            }
    
    public String getLabelTopping() {
        return label_topping;
            }
    public void setLabelTopping(String food_label) {
        label_topping = food_label;
            }
    
        public String getTOPPINGFOODSIZE() {
            return topping_food_size_name;
            }
        public void setTOPPINGFOODSIZE(String topping_food_size) {
            topping_food_size_name = topping_food_size;
            }
    
                public float getTOPPINGFOODPRICE() {
                    return topping_food_size_price;
                    }
                public void setTOPPINGFOODPRICE(float topping_food_price) {
                    topping_food_size_price = topping_food_price;
                    }
    
                        public int getTOPPINGID() {
                            return topping_id;
                                }
                        public void setTOPPINGID(int id) {
                            topping_id = id;
                                }
    public String getTOPPINGTITLE() {
        return topping_title;
        }
    public void setTOPPINGTITLE(String title) {
        topping_title = title;
        }
                    public float getTOPPINGPRICE() {
                        return topping_price;
                            }
                    public void setTOPPINGPRICE(float price) {
                        topping_price = price;
                            }
    @Override
    public int describeContents() {
        return 0;
    }
            @Override
            public void writeToParcel(Parcel dest, int flags) {
                dest.writeInt(topping_id);
                dest.writeString(topping_title);
                dest.writeFloat(topping_price);
                dest.writeString(topping_food_size_name);
                dest.writeFloat(topping_food_size_price);
                dest.writeString(label_topping);
                dest.writeFloat(price_topping);
                dest.writeList(food_topping_sizes);
            }
    public static final Parcelable.Creator<ToppingCombo> CREATOR = new Parcelable.Creator<ToppingCombo>() {
                public ToppingCombo createFromParcel(Parcel in) {
                    return new ToppingCombo(in);
                }
                public ToppingCombo[] newArray(int size) {
                    return new ToppingCombo[size];
                }
            };
                        @SuppressWarnings("unchecked")
                        private ToppingCombo(Parcel in) {
                            topping_id = in.readInt();
                            topping_title = in.readString();
                            topping_price = in.readFloat();
                            topping_food_size_name = in.readString();
                            topping_food_size_price = in.readFloat();
                            label_topping = in.readString();
                            price_topping = in.readFloat();
                            food_topping_sizes = in.readArrayList(Food_Topping_Items_Sizes.class.getClassLoader());
                        }
    public ToppingCombo() {
    }
    

    }

    Inner Json Model Class...
    

    public class Food_Topping_Items_Sizes implements Parcelable{

    private String label_topping;
    private float price_topping;
    private String size_topping;
    
                public String getSizeTopping() {
                     return size_topping;
                    }
                public void setSizeTopping(String food_size) {
                    size_topping = food_size;
                    }
    
                public float getPriceTopping() {
                    return price_topping;
                        }
                public void setPriceTopping(float food_price) {
                    price_topping = food_price;
                        }
    
                public String getLabelTopping() {
                    return label_topping;
                        }
                public void setLabelTopping(String food_label) {
                    label_topping = food_label;
                        }
    
    
    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
    
        dest.writeString(label_topping);
        dest.writeFloat(price_topping);
        dest.writeString(size_topping);
    }
    public static final Parcelable.Creator<Food_Topping_Items_Sizes> CREATOR = new Parcelable.Creator<Food_Topping_Items_Sizes>() {
        public Food_Topping_Items_Sizes createFromParcel(Parcel in) {
            return new Food_Topping_Items_Sizes(in);
        }
        public Food_Topping_Items_Sizes[] newArray(int size) {
            return new Food_Topping_Items_Sizes[size];
        }
    };
    private Food_Topping_Items_Sizes(Parcel in) {
    
        label_topping = in.readString();
        price_topping = in.readFloat();
        size_topping = in.readString();
        }
    public Food_Topping_Items_Sizes() {
    }
    

    }

    链接地址: http://www.djcxy.com/p/74942.html

    上一篇: 定时器的Android线程

    下一篇: 如何检索json嵌套数组数据并填充到listview中?