別のJSON配列内のJSON配列から複数のJSONオブジェクトを取得しますか?

Vawkx:

大学の割り当てのために私はよく知られているオランダのオンラインストアのAPIからアプリを検索その製品データを作成する必要があります。私は、新しい製品のオブジェクトに各製品のタイトル、概要、価格や画像のURLを格納する必要があります。これらの製品は、ArrayListのに格納されているとのArrayListが返されます。

製品アレイ内の各製品は、6個の製品イメージが含まれている「画像」と呼ばれるネストされた配列を有しています。これらの画像は、値としてキーやURLなどの画像サイズで、私のProductオブジェクトのHashMapの属性に格納する必要があります。しかし、私は右のそれを得るように見えることはできません。

クエリ「ポケモン」とJSONデータ:https://api.bol.com/catalog/v4/search/?apikey=25C4742A92BF468EB2BD888FC8FBFF40&format=json&q=pokemon

Productクラス:

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

私は私のアプリをテストするとき、すべての製品のHashMapのに最後の製品の画像のURLを格納しているようです。

ここでは、画像の説明を入力します。

私は時間のために自分のコードを見つめてきたと私はそれがこれを行う理由を見つけるように見えることはできません。私はおそらく非常に愚かな過ちを作ってるんだけど、私はちょうどそれが正確であるかを把握するように見えることはできません。

セミヨンダニロフ:

あなたはMap<String, String> imageUrls = new HashMap<>();間違った場所にあります。それはそうでなければ、同じ使用され、ループのためのあなたの最初の内にある必要がありMap、すべての製品について。

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

ちなみに、私が使用することをお勧めgsonライブラリを。それはあなたのコードより少ない定型を行います

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=4074&siteId=1