2016-07-08 5 views
-2

Ich analysiere die folgende JSON-Antwort, aber es gibt ein Problem beim Parsen, der Code, den ich versuche, analysiert nicht und ich bekomme keine Ausnahme oder Antwort.Wie analysiert man Array von Objekten in Android

Der Code, den ich beantragt hatte, es zu analysieren ist:

ArrayList<Model_BarcodeDetail> DownloadBarcode(String api_token) { 
      ArrayList<Model_BarcodeDetail> barcodeList = new ArrayList<Model_BarcodeDetail>(); 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost postRequest = new HttpPost(Utility.BASE_URL 
        + "?q=webservice/barcode_list&token=" + api_token +"&page="+"1" 
        + "&return=json"); 
      String url = Utility.BASE_URL 
        + "?q=webservice/barcode_list&token=" + api_token +"&page="+"1" 
        + "&return=json"; 
      System.out.println("======url::"+url); 
      String result = ""; 
      ArrayList<Model_BarcodeDetail> group_list = null; 
      ArrayList<Model_BarcodeList_Child> child_list = null; 
      try { 
       group_list = new ArrayList<Model_BarcodeDetail>(); 

       ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
       result = httpClient.execute(postRequest, responseHandler); 

       JSONObject root = new JSONObject(result); 
       JSONObject obj = root.getJSONObject(""); 
       System.out.println("====objjjj: "+obj.toString()); 
       JSONArray arraylist=obj.names(); 


       for (int i = 0; i < arraylist.length(); i++) { 
        child_list = new ArrayList<Model_BarcodeList_Child>(); 
        JSONObject jsonObj = arraylist.getJSONObject(i); 
        System.out.println("---json" + jsonObj); 
        Model_BarcodeDetail data = new Model_BarcodeDetail(); 
        data.setReference((jsonObj.getString("ref"))); 
        System.out.println("======refinprsing--"+data.getReference()); 
//     data.setName((jsonObj.getString("name"))); 
//     data.setDescription((jsonObj.getString("desc"))); 
//     data.setPrice((jsonObj.getString("price"))); 
//     data.setTotal(data.getPrice().trim()); 
//     data.setFixedTotal(data.getPrice().trim()); 
        JSONArray obj2 = jsonObj.getJSONArray("barcodes"); 

        for (int j = 0; j < obj2.length(); j++) { 
         JSONObject json = obj2.getJSONObject(j); 
         System.out.println("---json" + json); 
         Model_BarcodeList_Child data2 = new Model_BarcodeList_Child(); 
         data2.setBarcode((json.getString("barcode"))); 
         System.out.println("=======barcodeinparsing: "+data2.getBarcode()); 
         data2.setColor((json.getString("color"))); 
         data2.setSize((json.getString("size"))); 
         data2.setPrice(json.getString("price")); 
         data2.setStock(json.getString("stock")); 
         data2.setAlis_code(json.getString("alias_code")); 
         child_list.add(data2); 
        } 

        data.setChildItems(child_list); 
        group_list.add(data); 
       } 
       for (int k = 0; k < group_list.size(); k++) { 
        System.out.println("-----==data itemref " 
          + group_list.get(k).getReference()); 
        for (int y = 0; y < group_list.get(k).getChildItems().size(); y++) { 
         System.out.println("-----====data color " 
           + group_list.get(k).getChildItems().get(y) 
             .getColor()); 

        } 
       } 
      } catch (Exception e) { 
       Log.i("Exception in DownloadBarcodechanges method: ", e.getMessage()); 
       return null; 
      } 
      return group_list; 

     } 

Hier die json ist:

 { 
    "000002": { 
    "ref": "000002", 
    "barcodes": [ 
     { 
     "barcode": "000002014001", 
     "path": null, 
     "alias_code": null, 
     "color": "GREY", 
     "price": "10.50", 
     "size": "S", 
     "stock": "0" 
     }, 
     { 
     "barcode": "000002014002", 
     "path": null, 
     "alias_code": null, 
     "color": "GREY", 
     "price": "10.50", 
     "size": "M", 
     "stock": "2" 
     }, 
     { 
     "barcode": "000002014003", 
     "path": null, 
     "alias_code": null, 
     "color": "GREY", 
     "price": "10.50", 
     "size": "L", 
     "stock": "1" 
     }, 
     { 
     "barcode": "000002014004", 
     "path": null, 
     "alias_code": null, 
     "color": "GREY", 
     "price": "10.50", 
     "size": "XL", 
     "stock": "0" 
     }, 
     { 
     "barcode": "000002014005", 
     "path": null, 
     "alias_code": null, 
     "color": "GREY", 
     "price": "10.50", 
     "size": "XXL", 
     "stock": "1" 
     }, 
     { 
     "barcode": "000002014006", 
     "path": null, 
     "alias_code": null, 
     "color": "GREY", 
     "price": "13.50", 
     "size": "2XL", 
     "stock": "3" 
     }, 
     { 
     "barcode": "000002014007", 
     "path": null, 
     "alias_code": null, 
     "color": "GREY", 
     "price": "13.50", 
     "size": "3XL", 
     "stock": "5" 
     }, 
     { 
     "barcode": "000002014008", 
     "path": null, 
     "alias_code": null, 
     "color": "GREY", 
     "price": "13.50", 
     "size": "4XL", 
     "stock": "6" 
     }, 
     { 
     "barcode": "000002014009", 
     "path": null, 
     "alias_code": null, 
     "color": "GREY", 
     "price": "13.50", 
     "size": "5XL", 
     "stock": "5" 
     }, 
     { 
     "barcode": "000002014010", 
     "path": null, 
     "alias_code": null, 
     "color": "GREY", 
     "price": "10.50", 
     "size": "6XL", 
     "stock": "2" 
     } 
    ] 
    }, 
    "000012": { 
    "ref": "000012", 
    "barcodes": [ 
     { 
     "barcode": "000012030001", 
     "path": null, 
     "alias_code": null, 
     "color": "BLUE", 
     "price": "19.99", 
     "size": "S", 
     "stock": "1" 
     }, 
     { 
     "barcode": "000012030002", 
     "path": null, 
     "alias_code": null, 
     "color": "BLUE", 
     "price": "19.99", 
     "size": "M", 
     "stock": "3" 
     }, 
     { 
     "barcode": "000012030003", 
     "path": null, 
     "alias_code": null, 
     "color": "BLUE", 
     "price": "19.99", 
     "size": "L", 
     "stock": "4" 
     }, 
     { 
     "barcode": "000012030004", 
     "path": null, 
     "alias_code": null, 
     "color": "BLUE", 
     "price": "19.99", 
     "size": "XL", 
     "stock": "2" 
     }, 
     { 
     "barcode": "000012030005", 
     "path": null, 
     "alias_code": null, 
     "color": "BLUE", 
     "price": "19.99", 
     "size": "XXL", 
     "stock": "0" 
     }, 
     { 
     "barcode": "000012030006", 
     "path": null, 
     "alias_code": null, 
     "color": "BLUE", 
     "price": "19.99", 
     "size": "2XL", 
     "stock": "0" 
     }, 
     { 
     "barcode": "000012030007", 
     "path": null, 
     "alias_code": null, 
     "color": "BLUE", 
     "price": "19.99", 
     "size": "3XL", 
     "stock": "0" 
     }, 
     { 
     "barcode": "000012030008", 
     "path": null, 
     "alias_code": null, 
     "color": "BLUE", 
     "price": "19.99", 
     "size": "4XL", 
     "stock": "0" 
     }, 
     { 
     "barcode": "000012030009", 
     "path": null, 
     "alias_code": null, 
     "color": "BLUE", 
     "price": "19.99", 
     "size": "5XL", 
     "stock": "0" 
     }, 
     { 
     "barcode": "000012030010", 
     "path": null, 
     "alias_code": null, 
     "color": "BLUE", 
     "price": "19.99", 
     "size": "6XL", 
     "stock": "0" 
     } 
    ] 
    }, 
"pager": { 
    "current_page": 1, 
    "start": 0, 
    "limit": 50, 
    "total": "354", 
    "pages": 8 
    } 
} 
+0

Was ist diese Linie? JSONObject obj = root.getJSONObject (""); – user6547359

+0

ich kann nicht finden "ref", "farbe", "größe", "stock" etc in ihrem link .. überprüfen sie es einmal .. !! –

+0

Warum gibt es in Ihrem JSONObject null? – Zartha

Antwort

0

Basierend auf der json vorgesehen Änderung dieser:

JSONObject root = new JSONObject(result); 
JSONObject obj = root.getJSONObject(""); 
System.out.println("====objjjj: "+obj.toString()); 
JSONArray arraylist=obj.names(); 

zu dies:

 JSONObject root = new JSONObject(result); 
    JSONArray arraylist= root.names(); 

    for (int i = 0; i < arraylist.length(); i++) { 
        child_list = new ArrayList<Model_BarcodeList_Child>(); 
        JSONObject jsonObj = root.getJSONObject(arrayList.getString(i)); 

Ich würde auch eine Überprüfung hinzufügen, um zu sehen, ob der Name Pager entspricht, in diesem Fall fortfahren;

+0

Wie parse ich "000002": Tag, das dynamisch ist. Array beginnt nach "barcodes": tag –

+0

Ich glaube, du hast den richtigen json nicht verlinkt. Können Sie das überprüfen? – user6547359

+0

json hinzugefügt. Bitte überprüfe es jetzt. Vielen Dank! –

0

Nach Ihrer JSON-Antwort kann wie unten Parsen

JSONObject root = new JSONObject(result); 
JSONArray array = root.getJSONArray("array"); 
String boolean = root.getString("boolean"); 
String null= root.getString("null"); 
String number= root.getString("number"); 
String string = root.getString("string"); 
JSONArray array_1 = root.getJSONArray("object"); 

zuerst überprüfen Sie Ihr Ergebnis nicht null bekommt? Bitte überprüfen Sie das Protokoll.

0

Hier Lösung

JSONObject root = new JSONObject(result); 

    if(root != null){ 
     Iterator iter = root.keys(); 

     while (iter.hasNext()){ 
      try { 
       String key = (String) iter.next(); 

       JSONObject itemObject = root.getJSONObject(key); 


      } catch (JSONException e) { 

      } 
     } 
    } 
Verwandte Themen