如何解析Android中的对象数组

时间:2016-07-08 10:48:44

标签: android json parsing

我正在解析以下json响应但是在解析时存在一些问题,我尝试的代码没有解析,我没有得到任何异常或响应。

我申请解析它的代码是:

    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:

      {
  "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
  }
}

3 个答案:

答案 0 :(得分:0)

基于json提供的更改:

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

我还会添加一个检查以查看该名称是否等于寻呼机,在这种情况下继续;

答案 1 :(得分:0)

根据你的json Response做解析如下

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

首先检查你的结果是否为空?请查看日志。

答案 2 :(得分: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) {

            }
        }
    }