2012-03-20 35 views
0

我需要解析下面给出的JSON数据。如何解析JSON字符串的值为数组

{"result":[{"bookId":142645,"bookpb":"MF", 
"bookTs":1328999630000,"clipStatus":"D","bookDetail":{"arrival":1,"purchase":1,"sold":1}, 
"hierarchies":{"categories":["4"],"events":[]},"shopId":769752}, 

upto“sold”it working fine.but当我试图解析类别它不工作。下面给出的 是解析数据的代码。

ArrayList<BookItem> resultdata = new ArrayList<BookItem>(); 
JSONArray jsonArray = (new JSONObject(inputString)) 
    .getJSONArray("result"); 

for (int i = 0; i < jsonArray.length(); i++) { 

JSONObject jsonObject = jsonArray.getJSONObject(i); 
item = new BookItem(); 
item.setbookId(jsonObject.optString(book_ID)); 
item.setPurchase(jsonObject.optInt(PURCHASE)); 
item.setArrival(jsonObject.optInt(ARRIVAL)); 
item.setSold(jsonObject.optInt(SOLD)); 
item.setbookTs(jsonObject.optString(book_TS)); 
JSONObject hierarchies=jsonObject.getJSONObject(HIERARCHY); 
item.setCategory(hierarchies.getInt("categories")); 
resultdata.add(item); 
    } 

任何人都可以帮我吗?

我才知道,这是

{"categories":["4"],"events":[]} 

data.how的我可以分析该数组值的问题?

+0

它始终是最好用HTTP检查您的JSON字符串:// jsonlint。 com /如果它很复杂。这似乎是提到的一个是不正确的格式 – dhaval 2012-03-20 11:56:29

+0

PLZ帮助我..我卡在这.. – 2012-03-20 12:51:53

回答

0

类是JSONArray为了得到JSONArray

更换

item.setCategory(hierarchies.getInt("categories")); 

item.setCategory(hierarchies.getJSONArray("categories").getInt(0)); 
+0

有没有JSON数组,所以它不工作:( – 2012-03-20 13:14:20

+0

对不起,我的错误,我已经更新了代码PLZ参考它 – 2012-03-20 13:21:37

+0

Hurreyyyyyyyyyy .....它正在工作......非常感谢你Nixit ...... :) – 2012-03-20 13:36:13