如何从一个嵌套的JSON数组中检索多个JSON对象?

4
作为大学作业,我需要创建一个应用程序,从荷兰著名的在线商店API中检索产品数据。我需要将每个产品的标题、摘要、价格和图像URL存储到新的Product对象中。这些产品存储在ArrayList中,然后返回ArrayList。
产品数组中的每个产品都有一个名为“images”的嵌套数组,其中包含6个产品图像。这些图像需要存储到我的Product对象的HashMap属性中,以图像大小作为键,URL作为值。但是,我似乎无法做到这一点。
使用查询“pokemon”的JSON数据:https://api.bol.com/catalog/v4/search/?apikey=25C4742A92BF468EB2BD888FC8FBFF40&format=json&q=pokemon 产品类:
package com.example.bolcombrowser.domain;

import java.util.Map;

public class Product {

    // Attributes
    private String mTitle;
    private String mSummary;
    private double mPrice;
    private Map < String, String > mImageUrls;

    // Constructor
    public Product(String mTitle, String mSummary, double mPrice, Map < String, String > mImageUrls) {
        this.mTitle = mTitle;
        this.mSummary = mSummary;
        this.mPrice = mPrice;
        this.mImageUrls = mImageUrls;
    }

    // Getters and Setters
    public String getmTitle() {
        return mTitle;
    }

    public void setmTitle(String mTitle) {
        this.mTitle = mTitle;
    }

    public String getmSummary() {
        return mSummary;
    }

    public void setmSummary(String mSummary) {
        this.mSummary = mSummary;
    }

    public double getmPrice() {
        return mPrice;
    }

    public void setmPrice(double mPrice) {
        this.mPrice = mPrice;
    }

    public Map < String, String > getImageUrls() {
        return mImageUrls;
    }

    public void setImageUrls(Map < String, String > imageUrls) {
        this.mImageUrls = imageUrls;
    }
}

解析JSON方法:
public static ArrayList < Product > parseJson(String productJsonStr) throws JSONException {

    /* JSON array names. */
    final String BOL_PRODUCTS = "products";
    final String BOL_IMAGES = "images";
    final String BOL_OFFERS = "offers";

    /* JSON key names. */
    final String BOL_TITLE = "title";
    final String BOL_SUMMARY = "summary";
    final String BOL_OFFERDATA = "offerData";
    final String BOL_PRICE = "price";
    final String BOL_KEY = "key";
    final String BOL_URL = "url";

    /* Variables to store product data into, and is then used to create new Product objects. */
    String title;
    String summary;
    double price;
    Map < String, String > imageUrls = new HashMap < > ();

    /* ArrayList to store products into. */
    ArrayList < Product > productList = new ArrayList < > ();

    JSONObject productsJson = new JSONObject(productJsonStr);

    JSONArray productsArray = productsJson.getJSONArray(BOL_PRODUCTS);

    for (int i = 0; i < productsArray.length(); i++) {
        JSONObject product = productsArray.getJSONObject(i);

        /* Retrieve the title and summary of each product. */
        title = product.getString(BOL_TITLE);
        summary = product.getString(BOL_SUMMARY);

        JSONArray imagesArray = product.getJSONArray(BOL_IMAGES);

        for (int j = 0; j < imagesArray.length(); j++) {
            JSONObject image = imagesArray.getJSONObject(j);

            /* Retrieve each product's image sizes and URLs and store them into a HashMap. */
            String imageSize = image.getString(BOL_KEY);
            String imageUrl = image.getString(BOL_URL);

            imageUrls.put(imageSize, imageUrl);
        }

        JSONObject offerData = product.getJSONObject(BOL_OFFERDATA);
        JSONArray offers = offerData.getJSONArray(BOL_OFFERS);
        JSONObject offer = offers.getJSONObject(0);
        price = offer.getDouble(BOL_PRICE);

        productList.add(new Product(title, summary, price, imageUrls));
    }

    return productList;
}

onPostExecute方法:

@Override
protected void onPostExecute(String productData) {
    if (productData != null) {
        ArrayList < Product > productList;

        try {
            productList = JsonUtils.parseJson(productData);

            for (Product product: productList) {
                String title = product.getmTitle();
                String summary = product.getmSummary();
                double price = product.getmPrice();
                String hashMap = product.getImageUrls().toString();

                mTextViewOutput.append(title + "\n\n" + summary + "\n\n" + price + "\n\n" +
                    hashMap + "\n\n\n\n\n");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

当我测试我的应用程序时,似乎它已将最后一个产品的图像URL存储到每个产品的HashMap中:

enter image description here

我已经盯着代码看了几个小时,但似乎找不出它为什么会这样。我可能犯了一个非常愚蠢的错误,但我似乎无法确定它到底是什么。

1个回答

3

你的Map<String, String> imageUrls = new HashMap<>();放错了位置。它应该在你的第一个循环内部,否则你会为所有产品使用同一个Map

...
for (int i = 0; i < productsArray.length(); i++) {
    Map<String, String> imageUrls = new HashMap<>();
...

顺便提一下,我建议使用gson库。它可以让你的代码更简洁。


非常感谢!问题已经解决。我一定会研究gson的。 - Stefan Jaspers

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接