当 return 为数组时,JSONObject 无法转换为 JSONArray

JSONObject cannot be converted to JSONArray when the return is array

它向我显示以下错误:

JSON对象无法转换为JSON数组

这是我的代码:

JSONArray jsonArray = jsonObject.getJSONArray("GetTabDataResult");

我的JSON结果:

{"GetTabDataResult":{"TabId":1,"Bands":[{"Id":1,"ImageURL":"http:\/\/www.yallaasouq.com\/bands\/1.jpg","BrandId":0,"Men":true,"Women":true,"Kids":true,"Accessories":true,"Disabled":false,"Title":"","Type":0,"TypeId":0},{"Id":2,"ImageURL":"http:\/\/www.yallaasouq.com\/bands\/2.jpg","BrandId":0,"Men":true,"Women":true,"Kids":true,"Accessories":true,"Disabled":false,"Title":"","Type":0,"TypeId":0},{"Id":3,"ImageURL":"http:\/\/www.yallaasouq.com\/bands\/3.jpg","BrandId":0,"Men":true,"Women":true,"Kids":true,"Accessories":true,"Disabled":false,"Title":"","Type":0,"TypeId":0},{"Id":4,"ImageURL":"http:\/\/www.yallaasouq.com\/bands\/4.jpg","BrandId":0,"Men":true,"Women":true,"Kids":true,"Accessories":true,"Disabled":false,"Title":"","Type":0,"TypeId":0}],"Brands":[{"Id":4,"ArName":"Fabillo","EnName":"Fabillo","Code":"","Description":"","ImageURL":"http:\/\/www.yallaasouq.com\/brands\/fabillo.jpg","Men":true,"Women":false,"Kids":false,"Accessories":false,"FacebookLink":"","InstagrmLink":"","Disabled":false},{"Id":7,"ArName":"Carolina Boix","EnName":"Carolina Boix","Code":"","Description":"","ImageURL":"http:\/\/www.yallaasouq.com\/brands\/eva.jpg","Men":true,"Women":true,"Kids":false,"Accessories":false,"FacebookLink":"","InstagrmLink":"","Disabled":false},{"Id":9,"ArName":"Sport Master","EnName":"Sport Master","Code":"","Description":"","ImageURL":"http:\/\/www.yallaasouq.com\/brands\/sportmaster.jpg","Men":true,"Women":true,"Kids":true,"Accessories":false,"FacebookLink":"","InstagrmLink":"","Disabled":false},{"Id":10,"ArName":"Bershka","EnName":"Bershka","Code":"","Description":"","ImageURL":"http:\/\/www.yallaasouq.com\/brands\/eva.jpg","Men":true,"Women":true,"Kids":true,"Accessories":true,"FacebookLink":"","InstagrmLink":"","Disabled":false},{"Id":11,"ArName":"NO EXCESS","EnName":"NO EXCESS","Code":"","Description":"","ImageURL":"http:\/\/www.yallaasouq.com\/brands\/noexcess.jpg","Men":true,"Women":true,"Kids":true,"Accessories":true,"FacebookLink":"","InstagrmLink":"","Disabled":false}],"Categories":[{"Id":8,"EnName":"T-Shirts","ArName":"بلوزات","Code":"8","Men":true,"Women":true,"Kids":true,"Accessories":false,"MenImgURL":"http:\/\/www.yallaasouq.com\/Categories\/tshirt01.jpg","WomenImgURL":"","KidsImgURL":"","AccessoriesImgURL":""},{"Id":14,"EnName":"Shoes","ArName":"احذية","Code":"14","Men":true,"Women":true,"Kids":true,"Accessories":false,"MenImgURL":"http:\/\/www.yallaasouq.com\/Categories\/shoes01.jpg","WomenImgURL":"","KidsImgURL":"","AccessoriesImgURL":""},{"Id":17,"EnName":"Accessories","ArName":"اكسسوارت","Code":"17","Men":true,"Women":true,"Kids":false,"Accessories":false,"MenImgURL":"http:\/\/www.yallaasouq.com\/Categories\/accessories01.jpg","WomenImgURL":"","KidsImgURL":"","AccessoriesImgURL":""},{"Id":6833,"EnName":"Pants","ArName":"بنطلونات","Code":"6833","Men":true,"Women":true,"Kids":true,"Accessories":false,"MenImgURL":"http:\/\/www.yallaasouq.com\/Categories\/pants01.jpg","WomenImgURL":"","KidsImgURL":"","AccessoriesImgURL":""},{"Id":6834,"EnName":"Shirts","ArName":"قمصان","Code":"6834","Men":true,"Women":true,"Kids":true,"Accessories":false,"MenImgURL":"http:\/\/www.yallaasouq.com\/Categories\/hoodie01.jpg","WomenImgURL":"","KidsImgURL":"","AccessoriesImgURL":""}]}}

我知道我的 json 有子 json 对象(Bands、Brands、Categories),但在主对象 (GetTabDataResult) 中有 (TabId),它是一个值而不是子对象。

I also checked the service result in the fiddler and i attached here a photo of the screen...you can check it.

我在您提供的 Json 字符串中没有看到 Json 数组,这是一个 json 对象。 Json 数组被 [ ].

包围

所以改为将其转换为普通的 JSONObject:

jsonObject.getJSONObject("GetTabDataResult");

使用

JSONObject jsonOb = jsonObject.getJSONObject("GetTabDataResult");

而不是

JSONArray jsonArray = jsonObject.getJSONArray("GetTabDataResult");

这是 jsonarray

Bands, Brands, Categories

JsonArray bands =jsonOb.getJsonArray("Bands");

希望以上代码对您有所帮助!