我正在使用此代码
private void parseData(JSONArray array){
Log.d(TAG, "Parsing array");
for(int i = 0; i<array.length(); i++) {
bookItems bookItem = new bookItems();
JSONObject jsonObject = null;
try {
jsonObject = array.getJSONObject(i);
JSONObject bookChapter = jsonObject.getJSONObject("chapter");
bookItem.setbook_subtitle(bookChapter.getString("subtitle"));
JSONObject chapVerses = jsonObject.getJSONObject("verses");
JSONArray verseReaders = chapVerses.getJSONArray("readers");
JSONObject readersNum = verseReaders.getJSONObject("number");
verseReadNum = readersNum;
} catch (JSONException w) {
w.printStackTrace();
}
mbookItemsList.add(bookItem);
}
}
解析这个json。
[
{
"chapter": {
"subtitle": "Something happened in this in this chapter"
},
"verses": {
"about": [
{
"In this verse, a lot of things happened yes a lot!"
}
],
"readers": [
{
"read": false,
"number": "No body has read this verse yet"
}
],
}
},
...]
我正确地获得了“副标题”,但我很难获得“数字”。
来自 JSONObject readerNum = verseReaders.getJSONObject("number");
Android studio 提示 JSONArray 中的 getJSONOBJECT (int) 无法应用于 (java.lang.String)
请问我该如何正确解析这个?
最佳答案
verseReaders
是一个 JSONArray
,因此您需要迭代(或获取第一个)JSONObject
,然后从该对象获取字符串.
String readersNum = verseReaders.getJSONObject(0).getString("number");
关于java - 如何解析嵌套在 json 数组中的 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36729961/