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

时间:2015-09-18 13:00:32

标签: android json listview

  1. 将Json嵌套数组数据导入Arraylist。
  2. 将其填充到列表视图
  3. 还有Spinner,每当微调器数据更改时,列表视图数据也会发生变化..
  4. 这可以在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"
        }
      ]
    }
    

    这是我的json格式和Food_Sizes数组我要显示到Spinner(单选)...食品价格及其名称希望显示到listview ...每当微调器值改变列表视图值的价格也改变了...... 我获取此数据的代码......

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

    我在listview中显示如下..

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

    我的微调器显示如下......

    if(string_spinner_size != null && string_spinner_size.size() > 1) {
        spinner_size.setVisibility(View.VISIBLE);
        spinner_size.setAdapter(new ArrayAdapter<String>(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); 
    }
    

3 个答案:

答案 0 :(得分:1)

您可以使用nest for循环来获取嵌套字符串,如下所示,

     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

答案 1 :(得分:-1)

这实际上是3个不同的问题但是 1.您应该使用JSONObject,它为您提供解析json所需的所有功能:

JSONObject object = new JSONObject(YOUR_STRING);

现在你可以用这个对象做任何你想做的事情,如:

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

答案 2 :(得分:-1)

是..最后我找到了解决方案......

 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实现了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() {
}

}