Android Volley解析嵌套数组

时间:2019-02-03 18:46:36

标签: android android-volley

这是我收到的回复:

{
"orders": [
    {
        "status": "paid",
        "products": [
            "[{\"_id\":\"5c3e59d21ef408162063cb1b\",\"date\":\"Jan 16, 2019 2:08:18 AM\",\"imagelinks\":[\"https://images.express.com/is/image/expressfashion/0091_07108221_0020?cache=on&wid=361&fmt=jpeg&qlt=75,1&resmode=sharp2&op_usm=1,1,5,0&defaultImage=Photo-Coming-Soon\",\"https://images.express.com/is/image/expressfashion/0091_07108221_0020?cache=on&wid=361&fmt=jpeg&qlt=75,1&resmode=sharp2&op_usm=1,1,5,0&defaultImage=Photo-Coming-Soon\",\"https://images.express.com/is/image/expressfashion/0091_07108221_0020?cache=on&wid=361&fmt=jpeg&qlt=75,1&resmode=sharp2&op_usm=1,1,5,0&defaultImage=Photo-Coming-Soon\",\"https://gloimg.samcdn.com/S/pdm-product-pic/Clothing/2017/08/16/source-img/20170816102536_11718.jpg\"],\"likes\":0,\"maincategory\":\"Women\",\"maincategoryid\":\"5c3db82846d4af9bcc3e9b6f\",\"price\":500.0,\"productname\":\"Ripped Jeans\",\"quantity\":1,\"subcategory\":\"Jeans\",\"subcategoryid\":\"5c3db96446d4af9bcc3e9b78\"}]"
        ],
        "productsobject": [
            "[_id='5c3e59d21ef408162063cb1b', likes=0, imagelinks=[https://images.express.com/is/image/expressfashion/0091_07108221_0020?cache=on&wid=361&fmt=jpeg&qlt=75,1&resmode=sharp2&op_usm=1,1,5,0&defaultImage=Photo-Coming-Soon, https://images.express.com/is/image/expressfashion/0091_07108221_0020?cache=on&wid=361&fmt=jpeg&qlt=75,1&resmode=sharp2&op_usm=1,1,5,0&defaultImage=Photo-Coming-Soon, https://images.express.com/is/image/expressfashion/0091_07108221_0020?cache=on&wid=361&fmt=jpeg&qlt=75,1&resmode=sharp2&op_usm=1,1,5,0&defaultImage=Photo-Coming-Soon, https://gloimg.samcdn.com/S/pdm-product-pic/Clothing/2017/08/16/source-img/20170816102536_11718.jpg], productname='Ripped Jeans', price=500.0, subcategory='Jeans', subcategoryid='5c3db96446d4af9bcc3e9b78', maincategory='Women', maincategoryid='5c3db82846d4af9bcc3e9b6f', quantity=1, date=Wed Jan 16 02:08:18 GMT+04:00 2019}]"
        ],
        "_id": "5c5729008e13068d24eddda4",
        "fullname": "",
        "email": "kazm98@gmail.com",
        "state": "",
        "city": "",
        "address1": "",
        "address2": "",
        "zippostal": "",
        "mobilenumber": null,
        "userid": "5c49aeafa3160f850cf418ba",
        "totalprice": 560,
        "productsprice": 500,
        "shippingprice": 10,
        "tax": 50,
        "__v": 0
    }
]

}

我有自定义对象(PModel),但是我需要访问productsobject嵌套数组,这是我使用的方法:

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            url,jsonBody, response -> {
                Gson gson = new Gson();

                JSONArray jsonArray = response.optJSONArray("orders");
                JSONArray products = jsonArray.optJSONArray("productsobject");
                Type productsType = new TypeToken<Collection<PModel>>(){}.getType();
                Collection<PModel> productsList = gson.fromJson(String.valueOf(products), productsType);

                for(PModel pModel: productsList) {
                    Log.e(TAG, "productsobject: product name:"+pModel.getProductname());
                }
            }

但是我不能访问阵列吗?

0 个答案:

没有答案