从JSON对象中提取信息 - Android

时间:2015-11-16 04:11:30

标签: android json

我有一个JSON Feed,我需要从中提取“产品”信息,如下所示:更新

    {
    "category-key": "guvera-music",
    "category-localized-title": "Guvera music",
    "items": [
        {
            "type": "product",
            "product": {
                "engine-product-id": "com.guvera.nonrenewing.day",
                "localization-key": "guvera-1-day-pass",
                "type": "music.subscription",
                "localized-description": "Guvera 1 day pass",
                "category-key": "guvera-music",
                "denomination-currency": "IDR",
                "currency": "IDR",
                "product-promotion-message": "",
                "denomination-amount": 3000,
                "parent-category-id": null,
                "merchant": "Guvera",
                "price": 3300,
                "product-id": 1,
                "product-image-id": 19,
                "srp": 3300
            }
        },
        {
            "type": "product",
            "product": {
                "engine-product-id": "com.guvera.nonrenewing.week",
                "localization-key": "guvera-1-week-pass",
                "type": "music.subscription",
                "localized-description": "Guvera 1 week pass",
                "category-key": "guvera-music",
                "denomination-currency": "IDR",
                "currency": "IDR",
                "product-promotion-message": "",
                "denomination-amount": 20000,
                "parent-category-id": null,
                "merchant": "Guvera",
                "price": 22000,
                "product-id": 2,
                "product-image-id": 19,
                "srp": 22000
            }
        },
        {
            "type": "product",
            "product": {
                "engine-product-id": "com.guvera.nonrenewing.month",
                "localization-key": "guvera-1-month-pass",
                "type": "music.subscription",
                "localized-description": "Guvera 1 month pass",
                "category-key": "guvera-music",
                "denomination-currency": "IDR",
                "currency": "IDR",
                "product-promotion-message": "",
                "denomination-amount": 55000,
                "parent-category-id": null,
                "merchant": "Guvera",
                "price": 60500,
                "product-id": 3,
                "product-image-id": 19,
                "srp": 60500
            }
        },
        {
            "type": "product",
            "product": {
                "engine-product-id": "com.guvera.nonrenewing.trimester",
                "localization-key": "guvera-3-month-pass",
                "type": "music.subscription",
                "localized-description": "Guvera 3 month pass",
                "category-key": "guvera-music",
                "denomination-currency": "IDR",
                "currency": "IDR",
                "product-promotion-message": "",
                "denomination-amount": 165000,
                "parent-category-id": null,
                "merchant": "Guvera",
                "price": 181500,
                "product-id": 4,
                "product-image-id": 19,
                "srp": 181500
            }
        },
        {
            "type": "product",
            "product": {
                "engine-product-id": "com.guvera.nonrenewing.semester",
                "localization-key": "guvera-6-month-pass",
                "type": "music.subscription",
                "localized-description": "Guvera 6 month pass",
                "category-key": "guvera-music",
                "denomination-currency": "IDR",
                "currency": "IDR",
                "product-promotion-message": "",
                "denomination-amount": 303600,
                "parent-category-id": null,
                "merchant": "Guvera",
                "price": 333960,
                "product-id": 5,
                "product-image-id": 19,
                "srp": 333960
            }
        },
        {
            "type": "product",
            "product": {
                "engine-product-id": "com.guvera.nonrenewing.year",
                "localization-key": "guvera-12-month-pass",
                "type": "music.subscription",
                "localized-description": "Guvera 12 month pass",
                "category-key": "guvera-music",
                "denomination-currency": "IDR",
                "currency": "IDR",
                "product-promotion-message": "",
                "denomination-amount": 547800,
                "parent-category-id": null,
                "merchant": "Guvera",
                "price": 602580,
                "product-id": 6,
                "product-image-id": 19,
                "srp": 602580
            }
        }
    ]
}

我正在尝试使用以下代码,但我的代码无效,因此我可以获得“产品”:

            try {
            JSONArray jsonProductArray = response.getJSONArray("items");

            // Parse through
            for (int i = 0; i < jsonProductArray.length(); i++) {
            // Is the code below correct so that I can get "product"? 
                JSONObject products = jsonProductArray.getJSONObject(i).getJSONObject("product");
            }

这个JSON结构是一个包含对象的数组吗?或者更确切地说是一个包含带有6个项目的数组的对象我怎么能在上面的代码中隔离“产品”?任何有关如何解决这个问题的详细信息将不胜感激。

4 个答案:

答案 0 :(得分:0)

您上面粘贴的JSON不是有效的JSON。

要验证您的JSON是否正确,您可以使用JSONLint

  

这个JSON结构是一个包含对象的数组吗?或者更确切地说是一个包含带有6个项目的数组的对象?

请注意,如果你有一个JSON数组,你的JSON从[开始,以]结束,即方括号。在它里面你可以有JSON对象。 如果你有JSON对象,它将从{开始,以}结束,即Curly括号。

<强>更新

更新问题后,JSON有效。 现在我想你想要product JSON对象,因为你应该看到它在items JSON数组中。

所以你必须做这样的事情:

JSONArray jsonProductArray = response.getJSONArray("items");

JSONObject productObject=jsonProductArray.getJSONObject(2);

获取product

中的对象
productObject.get("nameOfObject");

希望它有所帮助。 :)

答案 1 :(得分:0)

try {
JSONArray jsonProductArray = response.getJSONArray("items");

// Parse through
for (int i = 0; i < jsonProductArray.length(); i++) {
// Is the code below correct so that I can get "product"? 
JSONObject products = jsonProductArray.getJSONObject(i).getJSONObject(1).getString("engine-product-id");
        }

答案 2 :(得分:0)

首先,你需要在root上获得JSONObject,见下文。

JSONObject j = new JSONObject(json);

此处json是您要解析的json文件或字符串。

然后,您需要获得名为JSONArray的{​​{1}},如下所示。

items

然后循环上面JSONArray a = j.getJSONArray("items"); ,如下所示。

JSONArray

答案 3 :(得分:0)

Gson库示例检索上面的json String,

您只需要一行来获取对象中填充的所有json数据,即

        Response response = (new Gson()).fromJson("your_json_String", Response.class);

要使此行工作,请将**gson**库添加到项目中并添加以下类

首先制作Product.java

import com.google.gson.annotations.SerializedName;

public class Product {

    @SerializedName("engine-product-id")
    private String engine_product_id;

    @SerializedName("localization-key")
    private String localization_key;

    @SerializedName("type")
    private String type;

    @SerializedName("localized-description")
    private String localized_description;

    @SerializedName("category-key")
    private String category_key;

    @SerializedName("denomination-currency")
    private String denomination_currency;

    @SerializedName("currency")
    private String currency;

    @SerializedName("product-promotion-message")
    private String product_promotion_message;

    @SerializedName("denomination-amount")
    private String denomination_amount;

    @SerializedName("parent-category-id")
    private String parent_category_id;

    @SerializedName("merchant")
    private String merchant;

    @SerializedName("price")
    private String price;

    @SerializedName("product-id")
    private String product_id;

    @SerializedName("product-image-id")
    private String product_image_id;

    @SerializedName("srp")
    private String srp;

    public String getEngine_product_id() {
        return engine_product_id;
    }

    public void setEngine_product_id(String engine_product_id) {
        this.engine_product_id = engine_product_id;
    }

    public String getLocalization_key() {
        return localization_key;
    }

    public void setLocalization_key(String localization_key) {
        this.localization_key = localization_key;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getLocalized_description() {
        return localized_description;
    }

    public void setLocalized_description(String localized_description) {
        this.localized_description = localized_description;
    }

    public String getCategory_key() {
        return category_key;
    }

    public void setCategory_key(String category_key) {
        this.category_key = category_key;
    }

    public String getDenomination_currency() {
        return denomination_currency;
    }

    public void setDenomination_currency(String denomination_currency) {
        this.denomination_currency = denomination_currency;
    }

    public String getCurrency() {
        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public String getProduct_promotion_message() {
        return product_promotion_message;
    }

    public void setProduct_promotion_message(String product_promotion_message) {
        this.product_promotion_message = product_promotion_message;
    }

    public String getDenomination_amount() {
        return denomination_amount;
    }

    public void setDenomination_amount(String denomination_amount) {
        this.denomination_amount = denomination_amount;
    }

    public String getParent_category_id() {
        return parent_category_id;
    }

    public void setParent_category_id(String parent_category_id) {
        this.parent_category_id = parent_category_id;
    }

    public String getMerchant() {
        return merchant;
    }

    public void setMerchant(String merchant) {
        this.merchant = merchant;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getProduct_id() {
        return product_id;
    }

    public void setProduct_id(String product_id) {
        this.product_id = product_id;
    }

    public String getProduct_image_id() {
        return product_image_id;
    }

    public void setProduct_image_id(String product_image_id) {
        this.product_image_id = product_image_id;
    }

    public String getSrp() {
        return srp;
    }

    public void setSrp(String srp) {
        this.srp = srp;
    }
}

现在制作Item.java

import com.google.gson.annotations.SerializedName;

public class Item {

    @SerializedName("type")
    private String type;

    @SerializedName("product")
    private String product;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getProduct() {
        return product;
    }

    public void setProduct(String product) {
        this.product = product;
    }
}

最后制作Response.java

import java.util.ArrayList;

import com.google.gson.annotations.SerializedName;

public class Response {

    @SerializedName("category-key")
    private String category_key;

    @SerializedName("category-localized-title")
    private String category_localized_title;

    @SerializedName("type")
    private ArrayList<Item> type;

    public String getCategory_key() {
        return category_key;
    }

    public void setCategory_key(String category_key) {
        this.category_key = category_key;
    }

    public String getCategory_localized_title() {
        return category_localized_title;
    }

    public void setCategory_localized_title(String category_localized_title) {
        this.category_localized_title = category_localized_title;
    }

    public ArrayList<Item> getType() {
        return type;
    }

    public void setType(ArrayList<Item> type) {
        this.type = type;
    }
}