2015-11-16 34 views
1

我有一個JSON飼料,我需要從中提取的「產品」的信息,如下圖所示:修訂從JSON對象中提取信息 - 安卓

{ 
    "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個項目的數組的對象?我怎麼能隔離上面的代碼中的「產品」?任何關於如何解決這個問題的詳細信息將不勝感激。

+2

是您的信息的響應的內容?因爲這不是真正的JSON – Virthuss

+2

不是一個有效的JSON字符串,如果JSON是由你創建的然後共享代碼從你正在生成發佈的JSON字符串 –

+0

你在這裏寫的是一個響應出現在HTTP客戶端程序,這個東西用於人類可讀的數據,但你必須得到原始的反應,複製,原始的是實際的JSON –

回答

0

你在上面粘貼的JSON是不是有效的JSON。

要驗證您的JSON是正確與否,你可以使用JSONLint

這是JSON結構內對象的數組?或者更確切地說,一個包含6個項目的數組的對象?

請注意,如果你有一個JSON數組的JSON從[開始,以]即方括號結束。在它裏面你可以有JSON對象。 如果你有JSON對象它將從{啓動和}即花括號結束。

UPDATE:

你更新的問題後,JSON是有效的。 現在,我想你想的product JSON對象,你應該看到它是items JSON數組中。

所以,你必須做這樣的事情:

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

JSONObject productObject=jsonProductArray.getJSONObject(2); 

要獲得product

productObject.get("nameOfObject"); 

內的對象希望它能幫助。 :)

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"); 
     } 
+0

試試這個代碼,讓我知道您的審查關於它:) –

0

首先你需要得到JSONObject在根上見下文。

JSONObject j = new JSONObject(json); 

這裏JSON是您json文件或字符串要解析。

然後您需要獲得JSONArray後,將其命名爲items,如下所示。

JSONArray a = j.getJSONArray("items"); 

然後通過上面的JSONArray循環,如下所示。

for(int i = 0; i < a.length(); i++){ 
    // Here you need to get JSONObject from JSONArray at Index i. 
    JSONOject jj = a.getJSONObject(i); 
    //Now you need to get product JSONObject 
    JSONObject jsonObj = jj.getJSONObject("product"); 
    //From jsonObj you get all information about product. 
} 
0

GSON庫的例子來獲取上述JSON字符串,

你只需要一個一線得到填充在你的對象,這是

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

這條線去上班了JSON數據將**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; 
    } 
}