2016-07-08 11 views
-2

次のjsonレスポンスを解析していますが、解析中に問題があり、解析しようとしているコードが解析されていないため、例外や応答が得られません。Androidでオブジェクトの配列を解析する方法

私はそれを解析するために適用されたコードは次のとおりです。ここで

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; 

     } 

は、JSONです:

JSON提供変化にこの基づい
 { 
    "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

のですか? JSONObject obj = root.getJSONObject( ""); – user6547359

+0

あなたのリンクに "ref"、 "color"、 "size"、 "stock"などが見つかりません。 –

+0

JSONObjectにnullがあるのはなぜですか? – Zartha

答えて

0

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

へのこれは:

 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)); 

また、名前がポケベルに等しいかどうかを確認するためのチェックを追加します。その場合は続行します。

+0

どのように私は "000002":動的なタグを解析するのですか? "バーコード"の後に配列が始まっています:タグ –

+0

正しいjsonをリンクしていないと思います。あなたは検証することができますか? – user6547359

+0

jsonが追加されました。今すぐご確認ください。ありがとう! –

0

は、あなたのJSONレスポンスによる

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"); 

最初に以下のように解析するには、あなたの結果がnullになっていませんチェックしますか?ログを確認してください。ここで

0

は、このラインは何ソリューション

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) { 

      } 
     } 
    } 
関連する問題