2016-07-25 25 views
0

它顯示我以下錯誤:的JSONObject不能轉換到JSONArray時,返回的是數組

的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,Br ands,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.

+4

'GetTabDataResult'是一個jsonobject而不是jsonArray –

回答

2

使用

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

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

這實際上是jsonarray

Bands, Brands, Categories

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

希望以上代碼對您有所幫助!

1

我看到您提供的JSON字符串沒有JsonArray,這是一個JSON對象。 Json陣列由[ ]圍攏。

因此,而不是將其轉換爲普通的JSONObject:

jsonObject.getJSONObject("GetTabDataResult"); 
相關問題