2013-01-11 62 views
0

json這樣解析嵌套的JSONObject和JSONArray android中

{ 
"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結果 - 在其他列表視圖。如何解決這個問題謝謝

回答

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

謝謝,但我想問如何獲得這個值與json, – user1920582

+0

編輯它的代碼 – user1969053