它显示以下错误:
JSONObject无法转换为JSONArray
这是我的代码:
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),它是一个值而不是一个子对象。
答案 0 :(得分:2)
使用
JSONObject jsonOb = jsonObject.getJSONObject("GetTabDataResult");
而不是
JSONArray jsonArray = jsonObject.getJSONArray("GetTabDataResult");
这是jsonarray
乐队,品牌,类别
JsonArray bands =jsonOb.getJsonArray("Bands");
希望以上代码对您有所帮助!
答案 1 :(得分:1)
我在你提供的Json String中看不到JsonArray,这是一个单独的json对象。 Json阵列由[ ]
代替。
所以改为将其转换为普通的JSONObject:
jsonObject.getJSONObject("GetTabDataResult");