【问题标题】:Retrieving multiple JSON objects from a JSON array within another JSON array?从另一个 JSON 数组中的 JSON 数组中检索多个 JSON 对象?
【发布时间】:2020-06-08 01:39:05
【问题描述】:

对于大学作业,我需要创建一个应用程序,该应用程序可以从一家著名的荷兰在线商店的 API 中检索产品数据。我需要将每个产品的标题、摘要、价格和图像 URL 存储到一个新的 Product 对象中。这些产品存储在一个 ArrayList 中,然后返回 ArrayList。

products 数组中的每个产品都有一个名为“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;
    }
}

parseJson 方法:

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 中:

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

【问题讨论】:

    标签: java android json


    【解决方案1】:

    您的Map&lt;String, String&gt; imageUrls = new HashMap&lt;&gt;(); 放错地方了。它应该在您的第一个 for 循环中,否则您对所有产品都使用相同的 Map

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

    顺便说一句,我建议使用gson 库。它将使您的代码更少样板

    【讨论】:

    • 非常感谢!那解决了它。我一定会调查 gson。
    猜你喜欢
    • 2018-08-26
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    相关资源
    最近更新 更多