繁体   English   中英

JSON Arraylist HashMap提取Android

[英]JSON Arraylist HashMap Extraction Android

请忍受这一点,按照完整的代码进行操作,我将把所有事情都弄清楚: 在此处输入图片说明

这是我的json:您可以在此处格式化

https://jsonformatter.curiousconcept.com/

[{"productLineItemId":5,"restaurantId":2,"productId":5,"catalogName":"Cold Drink","categoryName":"sprite","subCategoryName":"SPRITE ","productName":"SPRITE","price":20.0,"optionName":"no","optionValues":"200ML","veg":true,"spicy":false},{"productLineItemId":8,"restaurantId":2,"productId":5,"catalogName":"veg","categoryName":"south indian","subCategoryName":"rice","productName":"jeera Rice","price":888.0,"optionName":"notning","optionValues":"ooo","veg":true,"spicy":true},{"productLineItemId":100,"restaurantId":2,"productId":5,"catalogName":"non veg","categoryName":"south indian","subCategoryName":"hot briyani","productName":"briyani","price":9.0,"optionName":"plate","optionValues":"half","veg":true,"spicy":true}]

现在,在这里,您可以看到json内部现在有3个对象,这些对象将来会是动态的,意味着可能会有很多。所以,我的目标是检索所有这些Json对象并将它们显示在自定义适配器列表中。

这些下面是我的代码:MainActivity.java

 try {
            httpClient2=new DefaultHttpClient();
            StringBuilder stringBuilder2=new StringBuilder("xxxxxxxx");
            httpPost2=new HttpPost(stringBuilder2.toString());
            httpResponse2=httpClient2.execute(httpPost2);

            code=httpResponse2.getStatusLine().getStatusCode();
            httpPost2.setHeader(HTTP.CONTENT_TYPE,"application/json");

            HttpEntity httpEntity2=httpResponse2.getEntity();

            if(code<200 && code>=300){
                Log.d("msg","Here <200 and >300");
            }
            else{


  if(httpEntity2!=null){
                    cena= EntityUtils.toString(httpEntity2);

                    JSONArray jsonArray=new JSONArray(cena);

                    hashMapArrayList=new ArrayList<HashMap<String,String>>();

                    for(int i=0;i<jsonArray.length();i++) {
                        jsonObject = jsonArray.getJSONObject(i);

                        HashMap<String, String> hashMap = new HashMap<String, String>();

                        hashMap.put("Price", jsonObject.getString("price"));
                        hashMap.put("CategoryName", jsonObject.getString("categoryName"));
                        hashMap.put("ProductName", jsonObject.getString("productName"));
                        hashMap.put("CatalogName", jsonObject.getString("catalogName"));

                        hashMapArrayList.add(hashMap);

                    }



                    Intent intent = new Intent(MainActivity.this, Checkout.class);

                    intent.putExtra("arrayhash",hashMapArrayList);
                    startActivity(intent);


                    Log.d("cena","got something here"+cena.toString());
                }

//Checkout.java

 listView=(ListView)findViewById(R.id.listView);


     hashMapArrayList2=(ArrayList<HashMap<String,String>>) getIntent().getSerializableExtra("arrayhash");


    price_a = new String[hashMapArrayList2.size()];
    categoryName_b = new String[hashMapArrayList2.size()];
    productName_c = new String[hashMapArrayList2.size()];
    catalogName_d = new String[hashMapArrayList2.size()];

    int i=0;
    for(Map<String,String> item:hashMapArrayList2){
        price_a[i]=item.get("Price");
        categoryName_b[i]=item.get("CategoryName");
        productName_c[i]=item.get("ProductName");
        catalogName_d[i]=item.get("CatalogName");
        i++;
    }



    customAdapter=new CustomAdapter(Checkout.this,price_a,categoryName_b,productName_c,catalogName_d);
    listView.setAdapter(customAdapter);

//这是我的自定义适配器

public CustomAdapter(Context context,String price[],String categoryName[],String productName[],String catalogName[]){
    this.context=context;
    this.price=price;
    this.categoryName=categoryName;
    this.productName=productName;
    this.catalogName=catalogName;
}

@Override
public int getCount() {
    return price.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View v=layoutInflater.inflate(R.layout.custom_row,null);

    tv2=(TextView)v.findViewById(R.id.textView2);
    tv3=(TextView)v.findViewById(R.id.textView3);
    tv4=(TextView)v.findViewById(R.id.textView4);
    tv5=(TextView)v.findViewById(R.id.textView5);


    return v;
}

//所以我正在得到这样的输出: 在此处输入图片说明

//现在您可以看到问题了,我有3个json对象,但是我只能显示一个。请您告诉我需要修改的位置,以便我可以在自定义适配器中显示所有json对象(动态)我希望您能充分理解。我是开发新手,如果我犯了任何错误,对不起,深表感谢。

经过评论部分建议的修改后,我得到了这些:复制 在此处输入图片说明

只需如下修改您的逻辑

 hashMapArrayList=new ArrayList<HashMap<String,String>>();

 JSONArray jsonArray=new JSONArray(cena);

    for(int i=0;i<jsonArray.length();i++) {

       jsonObject = jsonArray.getJSONObject(i);
       HashMap<String, String> hashMap = new HashMap<String, String>();
       hashMap.put("Price", jsonObject.getString("price"));
       hashMap.put("CategoryName", jsonObject.getString("categoryName"));
       hashMap.put("ProductName", jsonObject.getString("productName"));
       hashMap.put("CatalogName", jsonObject.getString("catalogName"));
       hashMapArrayList.add(hashMap);

    }

只需在循环内移动HashMap<String, String> hashMap并将其一一添加到hashMapArrayList

修改后的代码。 在for循环之外初始化ArrayList ,并在for循环内部将HashMap添加到ArrayList

     hashMapArrayList=new ArrayList<HashMap<String,String>>();

   JSONArray jsonArray=new JSONArray(cena);
    for(int i=0;i<jsonArray.length();i++) {
                            jsonObject = jsonArray.getJSONObject(i);
                        HashMap<String, String> hashMap = new HashMap<String, String>();


                        hashMap.put("Price", jsonObject.getString("price"));
                        hashMap.put("CategoryName", jsonObject.getString("categoryName"));
                        hashMap.put("ProductName", jsonObject.getString("productName"));
                        hashMap.put("CatalogName", jsonObject.getString("catalogName"));

                        hashMapArrayList.add(hashMap);
      }

使用Gson进行解析

@Generated(“ org.jsonschema2pojo”)公共类示例{

@SerializedName("productLineItemId")
@Expose
private Integer productLineItemId;
@SerializedName("restaurantId")
@Expose
private Integer restaurantId;
@SerializedName("productId")
@Expose
private Integer productId;
@SerializedName("catalogName")
@Expose
private String catalogName;
@SerializedName("categoryName")
@Expose
private String categoryName;
@SerializedName("subCategoryName")
@Expose
private String subCategoryName;
@SerializedName("productName")
@Expose
private String productName;
@SerializedName("price")
@Expose
private Double price;
@SerializedName("optionName")
@Expose
private String optionName;
@SerializedName("optionValues")
@Expose
private String optionValues;
@SerializedName("veg")
@Expose
private Boolean veg;
@SerializedName("spicy")
@Expose
private Boolean spicy;

/**
 * @return The productLineItemId
 */
public Integer getProductLineItemId() {
    return productLineItemId;
}

/**
 * @param productLineItemId The productLineItemId
 */
public void setProductLineItemId(Integer productLineItemId) {
    this.productLineItemId = productLineItemId;
}

/**
 * @return The restaurantId
 */
public Integer getRestaurantId() {
    return restaurantId;
}

/**
 * @param restaurantId The restaurantId
 */
public void setRestaurantId(Integer restaurantId) {
    this.restaurantId = restaurantId;
}

/**
 * @return The productId
 */
public Integer getProductId() {
    return productId;
}

/**
 * @param productId The productId
 */
public void setProductId(Integer productId) {
    this.productId = productId;
}

/**
 * @return The catalogName
 */
public String getCatalogName() {
    return catalogName;
}

/**
 * @param catalogName The catalogName
 */
public void setCatalogName(String catalogName) {
    this.catalogName = catalogName;
}

/**
 * @return The categoryName
 */
public String getCategoryName() {
    return categoryName;
}

/**
 * @param categoryName The categoryName
 */
public void setCategoryName(String categoryName) {
    this.categoryName = categoryName;
}

/**
 * @return The subCategoryName
 */
public String getSubCategoryName() {
    return subCategoryName;
}

/**
 * @param subCategoryName The subCategoryName
 */
public void setSubCategoryName(String subCategoryName) {
    this.subCategoryName = subCategoryName;
}

/**
 * @return The productName
 */
public String getProductName() {
    return productName;
}

/**
 * @param productName The productName
 */
public void setProductName(String productName) {
    this.productName = productName;
}

/**
 * @return The price
 */
public Double getPrice() {
    return price;
}

/**
 * @param price The price
 */
public void setPrice(Double price) {
    this.price = price;
}

/**
 * @return The optionName
 */
public String getOptionName() {
    return optionName;
}

/**
 * @param optionName The optionName
 */
public void setOptionName(String optionName) {
    this.optionName = optionName;
}

/**
 * @return The optionValues
 */
public String getOptionValues() {
    return optionValues;
}

/**
 * @param optionValues The optionValues
 */
public void setOptionValues(String optionValues) {
    this.optionValues = optionValues;
}

/**
 * @return The veg
 */
public Boolean getVeg() {
    return veg;
}

/**
 * @param veg The veg
 */
public void setVeg(Boolean veg) {
    this.veg = veg;
}

/**
 * @return The spicy
 */
public Boolean getSpicy() {
    return spicy;
}

/**
 * @param spicy The spicy
 */
public void setSpicy(Boolean spicy) {
    this.spicy = spicy;
}

}

Example exampleObj = new Example();

Gson gson = new Gson();
JsonArray jsonArray = new JsonArray(response.string);

exampleObj = gson.fromJson(response.string,Example.class);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM