Android 如何解析值为数组的JSON字符串

Android 如何解析值为数组的JSON字符串,android,json,parsing,Android,Json,Parsing,我需要解析下面给出的JSON数据 {"result":[{"bookId":142645,"bookpb":"MF", "bookTs":1328999630000,"clipStatus":"D","bookDetail":{"arrival":1,"purchase":1,"sold":1}, "hierarchies":{"categories":["4"],"events":[]},"shopId":769752}, 直到“卖出”为止,它运行良好。但当我试图解析类别时,它不起作用。 下

我需要解析下面给出的JSON数据

{"result":[{"bookId":142645,"bookpb":"MF",
"bookTs":1328999630000,"clipStatus":"D","bookDetail":{"arrival":1,"purchase":1,"sold":1},
"hierarchies":{"categories":["4"],"events":[]},"shopId":769752},
直到“卖出”为止,它运行良好。但当我试图解析类别时,它不起作用。 下面给出了解析数据的代码

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);
  }

数据。如何解析此数组值?

对于您来说,最好的解决方案可能是GSON库。它自己进行序列化和反序列化,您将获得您的对象。

类别是一个JSONArray,用于获取JSONArray

替换

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


如果json字符串比较复杂,最好使用检查。上面提到的那个似乎格式不正确请帮帮我1.我被困在这个问题上了。。
item.setCategory(hierarchies.getInt("categories"));
item.setCategory(hierarchies.getJSONArray("categories").getInt(0));