2013-07-16 80 views
6

Tôi phải phân tích cú pháp dữ liệu mảng Json lồng nhau bên dưới vào ứng dụng của tôi. Tôi đang bối rối làm thế nào để có được các giá trị của nó.Cách phân tích mảng JSON lồng nhau này trong 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" 

         } 
      ] 
       }, 
      ] 
     } 

Bất kỳ ai cũng có thể hướng dẫn tôi cách lấy các giá trị bên trong từ đó.

Tôi đã cố gắng này

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 ok tôi sẽ cho bạn thấy. –

+2

Btw, json đó không hợp lệ. Mục cuối cùng trong đối tượng bên trong kết thúc bằng dấu phẩy: '" sku ":" 5555 ",' – Enrichman

+0

@Enrichman tôi đã thêm mã đã thử. Ngoài ra tôi đã xóa dấu phẩy –

Trả lời

19

Dưới đây là cách tôi nghĩ Parser JSON của bạn sẽ trông như thế (có thể có một số lỗi typo, tôi đã không kiểm tra mã này vào biên tập:)):

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

tôi đã chỉnh sửa JSON –

+0

Bạn đã thử nghiệm mã này nếu nó hoạt động? – hardartcore

+0

tôi sẽ kiểm tra nó.Cảm ơn sự giúp đỡ của bạn –

1

thử sử dụng mã

org.json.simple.JSONValue 

:

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

tôi đã chỉnh sửa JSON. –

1

Bạn có thể sử dụng thư viện GSon. Nó phân tích cú pháp json thành các đối tượng khác nhau mà bạn có thể truy cập. Mã giả ở đây:

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

Hãy thử cái này!

//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 reader = new JSONObject (kết quả);

  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

Đơn giản chỉ cần bạn có thể tự động phân tích nó với Gson:

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

Nhưng trên thực tế, bạn có thể phân tích các phản ứng JSON ở dạng chuỗi. Bạn không cần nhận phản hồi trong JSONObject và sử dụng toString().

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

Thông tin thêm về Gson: https://guides.codepath.com/android/Leveraging-the-Gson-Library


Đây là mẫu mô hình lớp, bạn có thể tạo ra nó bằng 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; 
    } 

} 
Các vấn đề liên quan