我想解析数组,但我无法从0 INDEX获取pdf文件。这是我的代码片段:
@Override
public void onResponse(JSONObject response) {
// response
Gson gson = new Gson() ;
MagazineResponseModel bookResponseModel;
bookResponseModel = gson.fromJson(response.toString(),MagazineResponseModel.class);
listGridViewBooks.addAll(bookResponseModel.getBooks());
mGridView.setAdapter(new MagazineGridViewAdapter(getActivity(),
R.layout.custom_books_view, listGridViewBooks));
}
答案 0 :(得分:2)
{
"file": [
"file1.pdf",
"file2.pdf"
]
}
这是获取json对象的最佳方法。
然后,你可以这样做....
JSONObject object = new JSONObject(jsonData);
System.out.println(object);
JSONArray file = object.getJSONArray("file");
System.out.println(file);
for (int i = 0; i < file.length(); i++) {
System.out.println(file.get(i));
}
它会打印出来。
{“file”:[“file1.pdf”,“file2.pdf”]}
[“file1.pdf”,“file2.pdf”]
file1.pdf
file2.pdf
答案 1 :(得分:1)
JSONArray fileArray = bookObject.getJSONArray("file");
for (int j = 0; j<fileArray.length(); j++){
JSONArray nestedFileArray = fileArray.getJSONArray(j);
YOUR_MODEL.setFile(nestedFileArray.getString(YOUR_FILE_POSITION));}
我重写@droiDev答案以获得更好的结果