courseName
名称很明确,我不能在模型类中添加任何值。
private static List<Country> countries = new ArrayList<>();
private ArrayList<String> courseName = new ArrayList<>();
JSONArray datalist = data.optJSONArray("data_list");
for (int i = 0; i < datalist.length(); i++) {
JSONObject jsonObject = datalist.getJSONObject(i);
/*strTitle.add(jsonObject.optString("cat_name_lan1"));
courceidtitle.add(jsonObject.optString("cat_id"));
courseName.clear();*/
strTitle = jsonObject.optString("cat_name_lan1");
JSONArray courseArray = jsonObject.optJSONArray("courses_array");
if (courseArray != null && courseArray.length() > 0) {
JSONObject courceName;
for (int j = 0; j < courseArray.length(); j++) {
courceName = courseArray.optJSONObject(j);
/*courceidchild.add(courceName.optString("cat_id"));*/
courseName.add(courceName.optString("course_name_lan1"));
}
countries.add(new Country(strTitle, courseName));
} else {
countries.add(new Country(strTitle, courseName));
}
courseName.clear();
}
我的JSON,
{
"data": {
"data_list": [
{
"cat_id": "5",
"cat_name_lan1": "Office Productivity",
"cat_name_lan2": "ऑफिस प्रोडक्टिविटी",
"cat_name_lan3": "ઓફિસ પ્રોડક્ટિવિટી",
"cat_name_lan4": null,
"cat_name_lan5": null,
"cat_name_lan6": null,
"courses_array": [
{
"course_id": "6",
"course_name_lan1": "Google Docs Primary Level ",
"course_name_lan2": "गुगल डॉक्स प्राथमिक स्तर",
"course_name_lan3": "ગુગલ ડોક્સ પ્રાથમિક સ્તર",
"course_name_lan4": "",
"course_name_lan5": "",
"course_name_lan6": "",
"course_take": 1
},
{
"course_id": "61",
"course_name_lan1": "Financial Function Excel 2016",
"course_name_lan2": "फाइनैन्शल फंक्शन एक्सेल 2016",
"course_name_lan3": "ફાઇનૈન્શલ ફંક્શન એક્સેલ 2016",
"course_name_lan4": "",
"course_name_lan5": "",
"course_name_lan6": "",
"course_take": 0
}
]
},
{
"cat_id": "4",
"cat_name_lan1": "I.T. & Software",
"cat_name_lan2": "आईटी & सोफ्टवेयर",
"cat_name_lan3": "આઈટી & સોફ્ટવેર",
"cat_name_lan4": null,
"cat_name_lan5": null,
"cat_name_lan6": null,
"courses_array": [
{
"course_id": "18",
"course_name_lan1": "Google Forms",
"course_name_lan2": "गुगल फॉर्म्स ",
"course_name_lan3": "ગુગલ ફોર્મ્સ ",
"course_name_lan4": "",
"course_name_lan5": "",
"course_name_lan6": "",
"course_take": 0
},
{
"course_id": "62",
"course_name_lan1": "Google Drive",
"course_name_lan2": "गुगल ड्राईव",
"course_name_lan3": "ગુગલ ડ્રાઈવ ",
"course_name_lan4": "",
"course_name_lan5": "",
"course_name_lan6": "",
"course_take": 1
}
]
}
],
"status": 0,
"message": "Category and courses tree view."
}
}
courseName
数组在for循环中是明确的,因此我在模型类中得到空白值。
答案 0 :(得分:0)
将您的代码更新为
JSONArray courseArray = jsonObject.optJSONArray("courses_array");
if (courseArray != null && courseArray.length() > 0) {
for (int j = 0; j < courseArray.length(); j++) {
// here you should initialize your courceName
JSONObject courceName = courseArray.optJSONObject(j);
courseName.add(courceName.optString("course_name_lan1"));
}
}
countries.add(new Country(strTitle, courseName));
答案 1 :(得分:0)
试试这段代码,
// Initialize countries list
List<Country> countries = new ArrayList<>();
JSONObject jObj = new JSONObject(json);
JSONObject data = jObj.optJSONObject("data");
JSONArray datalist = data.optJSONArray("data_list");
// Iterate data_list JSONArray
for (int i = 0; i < datalist.length(); i++) {
JSONObject jsonObject = datalist.getJSONObject(i);
String strTitle = jsonObject.optString("cat_name_lan1");
// Initialize courseName here
ArrayList<String> courseName = new ArrayList<>();
JSONArray courseArray = jsonObject.optJSONArray("courses_array");
// Iterate courses_array JSONArray
for (int j = 0; j < courseArray.length(); j++) {
JSONObject courceName = courseArray.optJSONObject(j);
courseName.add(courceName.optString("course_name_lan1"));
}
// Add strTitle and courseNames to countries list
countries.add(new Country(strTitle, courseName));
}