[英]Parse nested JSONObject and JSONArray in android
我有这样的json
{
"head": {
"title": "Music",
"status": "200"
},
"Info": [
{
"Name": "Mos Def",
"Type": "music",
"Results": [
{
"Name": "Talib Kweli",
"Type": "music"
},
{
"Name": "Black Star",
"Type": "music"
},
{
"Name": "Little Brother",
"Type": "music"
}
]
},
{
"Name": "Mos Def",
"Type": "Vehicles",
"Results": [
{
"Name": "Chevy",
"Type": "Car"
},
{
"Name": "Ford",
"Type": "Car"
},
{
"Name": "Pontiac",
"Type": "Car"
}
]
}
]
}
在我的代码中,我可以获取JSON对象信息,以在列表视图中显示类型“音乐,车辆”。 我想单击“音乐”,它将在其他列表视图中显示jsonObject结果。 该怎么解决? 谢谢
On click of setOnItemSelectedListener of your listView set adapter for another listView.
I worked on similar scenarion in my case I was getting json response for departments and corresponding Team for departments. Onclick of departments I have to show team list for selected department.I have used spinners for displaying items .Here is the sample code for that :
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, mDeptmntList);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mDeptmntSpinner.setAdapter(dataAdapter);
mDeptmntSpinner
.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
mSelectedDeptId = index;
ArrayAdapter<String> teamAdapter = new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_spinner_item, mList
.get(index));
teamAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mTeamSpinner.setAdapter(teamAdapter);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
this was my json response :
{
"departmentId":"1",
"departmentname":"HR",
"teamlist":[
{"teamName":"HR_S",
"teamName":"HR_P"}
]
}
Code for fetching this json is :
JSONArray array = new JSONArray(JSON response);
JSONObject obj;
mDeptmntList = new ArrayList<String>();
mTeam = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < array.length(); i++) {
obj = (JSONObject) array.get(i);
mDeptmntList.add(i, obj.getString("departmentname"));
JSONArray teamList = obj.getJSONArray("teamlist");
int count = teamList.length();
for (int j = 0; j < count; j++) {
mTeamTempId.add(j,teamList.getJSONObject(j).getString("teamName"));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.