-2

I am having problem in parsing the following response. It contains n number os objects in an object with xml format. How should I parse them?

{
  "000002": {
    "ref": "000002",
    "barcodes": {
      "data": "<sku><barcode>000002014001</barcode><path></path><alias_code></alias_code><color>GREY</color><price>10.50</price><size>S</size><stock>0</stock></sku><sku><barcode>000002014002</barcode><path></path><alias_code></alias_code><color>GREY</color><price>10.50</price><size>M</size><stock>2</stock></sku><sku><barcode>000002014003</barcode><path></path><alias_code></alias_code><color>GREY</color><price>10.50</price><size>L</size><stock>1</stock></sku><sku>,
      "#datatype": "xml"
    }
  },
  "000012": {
    "ref": "000012",
    "barcodes": {
      "data": "<sku><barcode>000012030001</barcode><path></path><alias_code></alias_code><color>BLUE</color><price>19.99</price><size>S</size><stock>1</stock></sku><sku><barcode>000012030002</barcode><path></path><alias_code></alias_code><color>BLUE</color><price>19.99</price><size>M</size><stock>3</stock></sku><sku>,
      "#datatype": "xml"
    }
  },
"pager": {
    "current_page": 1,
    "start": 0,
    "limit": 50,
    "total": "354",
    "pages": 8
  }
}

I am trying the below parsing code, but it is throwing exceptions. parsing code:

        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 jsonarray = new JSONObject(result);
            for (int i = 0; i < jsonarray.length(); i++) {
                child_list = new ArrayList<Model_BarcodeList_Child>();
                JSONObject jsonObj = jsonarray.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("skus");

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

        } catch (Exception e) {
            Log.i("Exception in DownloadBarcode method: ", e.getMessage());
            return null;
        }
        return group_list;
Sheena Tyagi
  • 341
  • 1
  • 4
  • 17

2 Answers2

0

You should use ksoap2 for parsing XML response. You can download JAR from here

check this for more info -

1) https://github.com/simpligility/ksoap2-android

2) How to call a SOAP web service on Android

3) how to Call ksoap web service from android?

Community
  • 1
  • 1
Onkar Nene
  • 1,359
  • 1
  • 17
  • 23
0

Apparently it's a JSON format , and then I copy this data in the http://jsoneditoronline.org/ it shows like the picture below enter image description here

So the data you have should be changed like this

{
  "000002": {
    "ref": "000002",
    "barcodes": {
      "data": "<sku><barcode>000002014001</barcode><path></path><alias_code></alias_code><color>GREY</color><price>10.50</price><size>S</size><stock>0</stock></sku><sku><barcode>000002014002</barcode><path></path><alias_code></alias_code><color>GREY</color><price>10.50</price><size>M</size><stock>2</stock></sku><sku><barcode>000002014003</barcode><path></path><alias_code></alias_code><color>GREY</color><price>10.50</price><size>L</size><stock>1</stock></sku><sku>",
      "#datatype": "xml"
    }
  },
  "000012": {
    "ref": "000012",
    "barcodes": {
      "data": "<sku><barcode>000012030001</barcode><path></path><alias_code></alias_code><color>BLUE</color><price>19.99</price><size>S</size><stock>1</stock></sku><sku><barcode>000012030002</barcode><path></path><alias_code></alias_code><color>BLUE</color><price>19.99</price><size>M</size><stock>3</stock></sku><sku>",
      "#datatype": "xml"
    }
  },
"pager": {
    "current_page": 1,
    "start": 0,
    "limit": 50,
    "total": "354",
    "pages": 8
  }
}
Nicholas Liu
  • 61
  • 1
  • 3