2016-06-20 23 views
-1

Ich habe die unten json Antwort.JSON Parsing in Android mit Volley Lib

[ 
{ 
    "id" : "001", 
    "firstName" : "Theo", 
    "lastName" : "Tziomakas", 
    "role" : "CEO", 
    "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
}, 
{ 
    "teamName" : "iOS", 
    "members" : [ 
     { 
      "id" : "002", 
      "firstName" : "Olly", 
      "lastName" : "Berry", 
      "role" : "iOS Team Lead", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png", 
      "teamLead" : true 
     }, 
     { 
      "id" : "003", 
      "firstName" : "James", 
      "lastName" : "Frost", 
      "role" : "iOS Developer", 
      "profileImageURL" : "http://developers.mub.lu/resources/profilePlaceholder.png" 
     } 
     ] 
    } 
] 

Und wollen das teamName JSON-Objekt und teamName JSON-Array lesen. Diese

ist, was ich tue,

public void onResponse(JSONArray response) { 
      Log.d("Theo", response.toString()); 

      for(int i = 0;i<response.length();i++){ 

       try { 
        //[{"id":"001","firstName":"Mark","lastName":"Mason","role":"CEO","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"}, 
        // {"teamName":"iOS","members":[{"id":"002","firstName":"Olly","lastName":"Berry","role":"iOS Team Lead","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png","teamLead":true},{"id":"003","firstName":"James","lastName":"Frost","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"004","firstName":"Liam","lastName":"Nichols","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"005","firstName":"Chris","lastName":"Watson","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"006","firstName":"Richard","lastName":"Turton","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"007","firstName":"Matt","lastName":"Colliss","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"008","firstName":"David","lastName":"Gibson","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"009","firstName":"Tom","lastName":"Guy","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"010","firstName":"Rich","lastName":"Hodgkins","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"}]},{"teamName":"Android","members":[{"id":"011","firstName":"David","lastName":"Branton","role":"Android Team Lead","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png","teamLead":true},{"id":"012","firstName":"Dre","lastName":"Pilipczuk","role":"Android Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"013","firstName":"Ray","lastName":"Britton","role":"Android Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"014","firstName":"Charly","lastName":"Murillo","role":"Android Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"}]},{"teamName":"Web","members":[{"id":"015","firstName":"Ryan","lastName":"French","role":"Web Team Lead","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png","teamLead":true},{"id":"016","firstName":"James","lastName":"Ward","role":"Web Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"018","firstName":"Adam","lastName":"Smith","role":"Web Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"019","firstName":"Leonard","lastName":"Da Costa","role":"Web Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"}]},{"teamName":"Design","members":[{"id":"020","firstName":"Hannah","lastName":"Tempest","role":"Design Team Lead","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png","teamLead":true},{"id":"021","firstName":"Ellis","lastName":"Reed","role":"Designer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"022","firstName":"Pete","lastName":"Horsham","role":"Designer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"023","firstName":"Hemel","lastName":"Dave","role":"Designer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"024","firstName":"Hannah","lastName":"Corke","role":"Designer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"}]}] 
        JSONObject jsonObject = response.getJSONObject(i); 

        for(int j=0;j<jsonObject.length();j++){ 
         JSONObject object = jsonObject.getJSONObject("teamName"); 
         Log.v("Theod",object.toString()); 




       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

Allerdings bekomme ich eine beschweren besagt, dass die „Teamname“ Objekt existiert nicht.

Irgendwelche Ideen dazu?

Danke.

Ich kann den TeamName für iOS :) lesen. Aber jetzt bin ich verwirrt mit dem Mitglieder-Array. Ich habe eine andere Schleife

@Override 
     public void onResponse(JSONArray response) { 
      Log.d("Theo", response.toString()); 

      for(int i = 0;i<response.length();i++){ 

       try { 
        //[{"id":"001","firstName":"Mark","lastName":"Mason","role":"CEO","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"}, 
        // {"teamName":"iOS","members":[{"id":"002","firstName":"Olly","lastName":"Berry","role":"iOS Team Lead","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png","teamLead":true},{"id":"003","firstName":"James","lastName":"Frost","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"004","firstName":"Liam","lastName":"Nichols","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"005","firstName":"Chris","lastName":"Watson","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"006","firstName":"Richard","lastName":"Turton","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"007","firstName":"Matt","lastName":"Colliss","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"008","firstName":"David","lastName":"Gibson","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"009","firstName":"Tom","lastName":"Guy","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"010","firstName":"Rich","lastName":"Hodgkins","role":"iOS Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"}]},{"teamName":"Android","members":[{"id":"011","firstName":"David","lastName":"Branton","role":"Android Team Lead","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png","teamLead":true},{"id":"012","firstName":"Dre","lastName":"Pilipczuk","role":"Android Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"013","firstName":"Ray","lastName":"Britton","role":"Android Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"014","firstName":"Charly","lastName":"Murillo","role":"Android Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"}]},{"teamName":"Web","members":[{"id":"015","firstName":"Ryan","lastName":"French","role":"Web Team Lead","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png","teamLead":true},{"id":"016","firstName":"James","lastName":"Ward","role":"Web Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"018","firstName":"Adam","lastName":"Smith","role":"Web Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"019","firstName":"Leonard","lastName":"Da Costa","role":"Web Developer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"}]},{"teamName":"Design","members":[{"id":"020","firstName":"Hannah","lastName":"Tempest","role":"Design Team Lead","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png","teamLead":true},{"id":"021","firstName":"Ellis","lastName":"Reed","role":"Designer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"022","firstName":"Pete","lastName":"Horsham","role":"Designer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"023","firstName":"Hemel","lastName":"Dave","role":"Designer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"},{"id":"024","firstName":"Hannah","lastName":"Corke","role":"Designer","profileImageURL":"http:\/\/developers.mub.lu\/resources\/profilePlaceholder.png"}]}] 
        JSONObject jsonObject = response.getJSONObject(1); 
        for(int j=0;j<jsonObject.length();j++){ 
         Model m = new Model(); 

         m.setRole(jsonObject.getString("teamName")); 


         JSONArray jsonArray = jsonObject.getJSONArray("members"); 


         modelArrayList.add(m); 
         for(int k = 0;k<jsonArray.length();k++){ 

          JSONObject teamMembers =    
      jsonArray.getJSONObject(i); 
      JSONObject teamMembers = jsonArray.getJSONObject(i); 

      m.setFirstName(teamMembers.getString("fistName")); 
          m.setSecondName(teamMembers.getString("lastName")); 

          modelArrayList.add(m); 
         } 
         Log.v("Theod",jsonArray.toString()); 
        } 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 

      // Update list by notifying the adapter of changes 
      myAdapter.notifyDataSetChanged(); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

      //hidePD(); 
     } 
    }); 
    AppController.getInstance().addToRequestQueue(jsObjRequest); 

} 

aber keine Daten angezeigt werden :(

+1

dies eine schlimme Datenstruktur Ihr Wurzelelement ist ein Array, das aus verschiedenen Typen besteht Ich schlage vor, dass Sie es für etwas ändern, bei dem das Wurzelelement ein Objekt ist, so dass Sie auf die verschiedenen t zugreifen können ypes mit Namen. – njzk2

+0

Ich weiß, dass es schrecklich ist, aber das ist, was ich gefragt wurde, und ich bin wie was ??? – Theo

+0

@ njzk2 Bitte lesen Sie meine aktualisierte Art der Antwort. – Theo

Antwort

0

ich eine ähnliche Frage here beantwortet haben, die glaube ich kann Ihnen helfen.

public String parseJSON(String jsonString) { 
try { 
    JSONObject object = (JSONObject) new JSONTokener(jsonString).nextValue(); 
    return object.getJSONObject(1).getString("teamName"); 
} 
catch (JSONException e) { 
    return null; 
} 

}