2013-07-16 67 views
6

我必須解析下面嵌套的Json數組的數據到我的應用程序。我很困惑如何從中獲得價值。如何解析這個嵌套的JSON數組android

{ 
      "prodCat_list": [ 
       { 
        "prods": [ 
         { 
          "cat_id": "9", 
          "position": "1", 
          "sku": "wwww345" 

         }, 
         { 
          "cat_id": "9", 
          "position": "2", 
          "sku": "coof23" 

         }, 
         { 
          "cat_id": "9", 
          "position": "3", 
          "sku": "dde45" 

         }, 
         { 
          "cat_id": "9", 
          "position": "4", 
          "sku": "5555" 

         } 
      ] 
       }, 
{ 
        "prods": [ 
         { 
          "cat_id": "9", 
          "position": "1", 
          "sku": "wwww345" 

         }, 
         { 
          "cat_id": "9", 
          "position": "2", 
          "sku": "coof23" 

         }, 
         { 
          "cat_id": "9", 
          "position": "3", 
          "sku": "dde45" 

         }, 
         { 
          "cat_id": "9", 
          "position": "4", 
          "sku": "5555" 

         } 
      ] 
       }, 
      ] 
     } 

任何人都可以請指導我如何從裏面得到的值。

我已經試過這

JSONParser parser = new JSONParser(); 
     JSONObject items = parser.getJSONFromUrl(productInfoUrl); 
     try { 
      JSONArray itemsDetails = items.getJSONArray("prodCat_list"); 
      if(itemsDetails.length()>0){ 

       for (int i = 0; i < itemsDetails.length(); i++) { 
        JSONArray productWithCategories = itemsDetails.getJSONArray(i); 
        JSONObject object = productWithCategories.getJSONObject(i); 

        Product productInfo = new Product(object.getString("sku"), object.getInt("cat_id"), object.getInt("position")); 
        ProductDbHandler productDbHandler = new ProductDbHandler(context); 
        productDbHandler.addProducts(productInfo); 
       } 
      } 
      else 
       System.out.println("No product to add"); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
+0

@Enrichman好的,我會告訴你。 –

+2

順便說一句,那json是無效的。內部對象的最後一項以逗號結尾:'「sku」:「5555」,「 – Enrichman

+0

@Enrichman」我添加了試用代碼。我也刪除了那個逗號 –

回答

19

這裏是我想你的JSON解析器應該像(可以有一些錯字的錯誤,我沒有測試在編輯此代碼:)):

JSONObject mainObj = new JSONOBject(myString); 
if(mainObj != null){ 
    JSONArray list = mainObj.getJSONArray("prodCat_list"); 
    if(list != null){ 
     for(int i = 0; i < list.length();i++){ 
      JSONObject elem = list.getJSONObject(i); 
      if(elem != null){ 
       JSONArray prods = elem.getJSONArray("prods"); 
       if(prods != null){ 
        for(int j = 0; j < prods.length();j++){ 
         JSONObject innerElem = prods.getJSONObject(j); 
         if(innerElem != null){ 
          int cat_id = innerELem.getInt("cat_id"); 
          int pos = innerElem.getInt("position"); 
          String sku = innerElem.getString("sku"); 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+0

我編輯了JSON –

+0

你測試了代碼是否正常工作? – hardartcore

+0

我會測試它。謝謝 –

1

嘗試使用

org.json.simple.JSONValue 

代碼:

import org.json.simple.JSONValue; 
String content = "{...}"; 
JSONValue.parse(content); 
+0

我編輯了JSON。 –

1

可以使用GSON庫。它將json解析爲可以訪問的各種對象。 一個虛擬代碼在這裏:

` GSon gSon = new GSon(); 
    ProdCatList prodCatList = gSon.fromJson(---inputStreamReader of your JSon data---,ProdCatList.class);` 
1

試試這個吧!

//Write your own implementation of json parser 
JSONParser jParser = new JSONParser(); 
JSONArray prod_cat = new JSONArray(); 
JSONArray products = new JSONArray(); 
JSONObject json = jParser.getJSONFromUrl("your source"); 
prod_cat = json.getJSONArray("prodCat_list"); 
for (int i = 0; i < prod_cat.length(); i++) { 
     JSONObject object = prod_cat.getJSONObject(i); 
     products = object.getJSONArray("products"); 
} 
0

的JSONObject讀卡器=新的JSONObject(結果);

  JSONArray VendorProductsList = reader.getJSONArray("VendorProductsList"); 
      for (int i = 0; i < VendorProductsList.length(); i++) { 
       ebay=new Ebay(); 
       JSONObject elem = VendorProductsList.getJSONObject(i); 
       JSONArray Products = elem.getJSONArray("Products"); 

       for (int j = 0; j < Products.length(); j++) { 
        JSONObject innerElem = Products.getJSONObject(j); 
        //JSONObject ProductName=Products.getJSONObject(j); 
        ebay.setProductName(innerElem.getString("ProductName")); 
        gallery_array.add(ebay); 
0

簡單的,你可以自動GSON解析它:

JSONParser parser = new JSONParser(); 
JSONObject items = parser.getJSONFromUrl(productInfoUrl); 
Gson gson = new GsonBuilder().create(); 
ProdCatResponse prodCatResponse = gson.fromJson(items.toString(), ProdCatResponse.class); 

但實際上,你可以解析字符串形式的JSON響應。您無需在JSONObject中獲得響應,並使用toString()

JSONParser parser = new JSONParser(); 
String items = parser.getFromUrl(productInfoUrl); 
Gson gson = new GsonBuilder().create(); 
ProdCatResponse prodCatResponse = gson.fromJson(items, ProdCatResponse.class); 

約GSON更多信息:https://guides.codepath.com/android/Leveraging-the-Gson-Library


這是模型類樣品,你可以用http://www.jsonschema2pojo.org生成它。

ProdCatResponse.java

public class ProdCatResponse { 

    private List <Prod> prods = new ArrayList <Prod>(); 

    public List <Prod> getProds() { 
     return prods; 
    } 

    public void setProds(List <Prod> prods) { 
     this.prods = prods; 
    } 

} 

Prod.java

public class Prod { 

    private String catId; 
    private String position; 
    private String sku; 

    public String getCatId() { 
     return catId; 
    } 

    public void setCatId(String catId) { 
     this.catId = catId; 
    } 

    public String getPosition() { 
     return position; 
    } 

    public void setPosition(String position) { 
     this.position = position; 
    } 

    public String getSku() { 
     return sku; 
    } 

    public void setSku(String sku) { 
     this.sku = sku; 
    } 

}