在android中解析嵌套的JSONObject和JSONArray

时间:2013-01-11 05:42:17

标签: android json

我有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结果 - 在其他列表视图中。这怎么解决?感谢

1 个答案:

答案 0 :(得分:0)

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