2017-03-01 41 views
0

我想知道我怎麼訪問Android的這個JSON的值:接取嵌套JSON的Android

{ 
    "dados": [{ 
     "Id": 3, 
     "IdChamado": 3, 
     "Chamado": "value", 
     "Solicitante": "value", 
     "Acao": "", 
     "ItemDeCatalogo": "Mobile | Instalação", 
     "InicioPrevisto": "06/01/2017 08:11:00", 
     "TerminoPrevisto": "06/01/2017 08:22:00" 
    }, { 
     "Id": 4, 
     "IdChamado": 4, 
     "Chamado": "value", 
     "Solicitante": "value", 
     "Acao": "", 
     "ItemDeCatalogo": "value", 
     "InicioPrevisto": "06/01/2017 08:11:34", 
     "TerminoPrevisto": "06/01/2017 08:11:34" 
    }], 
    "success": true, 
    "erroAplicacao": false 
} 

我需要訪問值 「IdChamado」, 「chamado」, 「Solicitante」例如。我已經看到嵌套數組的答案,但與jsonObjects有一個實際的名字,如this

PS:對不起,我忘了我發佈的代碼:

//Method called when the doInBack is complete 
    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 

     try { 
      JSONObject jsonObject = new JSONObject(result); 
      JSONArray jArray = jsonObject.getJSONArray("dados"); 
      Log.i("***2nd JSON ITSELF***", result); 

      for (int i=0; i<jArray.length(); i++) { 
        JSONObject jsonPart = jArray.getJSONObject(i); 

        int id = jsonPart.getInt("id"); 
        Log.i("***id***", String.valueOf(id)); 
        String chamado = jsonPart.getString("Chamado"); 
        Log.i("***Chamado***", chamado); 
        String solicitante = jsonPart.getString("solicitante"); 
        Log.i("***Solicitante***", solicitante); 
        String itemDeCatalogo = jsonPart.getString("itemDeCatalogo"); 
        Log.i("***Item de Catalogo***", itemDeCatalogo); 
       } 
     }catch(JSONException e) { 
      e.printStackTrace(); 
     }// END CATCH 
    }// END POST EXECUTE 

[解決]:謝謝你這麼多的人,你是我喜歡寫代碼的原因(不要怕問愚蠢的問題的)。這一切都與您發送回答的代碼一致。我認爲這會更復雜。的xD

@Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 

     try { 
      JSONObject jsonobject = new JSONObject(result); 
      JSONArray jsonarray = jsonobject.getJSONArray("dados"); 

      for (int i = 0; i < jsonarray.length(); i++) { 
       JSONObject jobject = jsonarray.getJSONObject(i); 
       String idChamado = jobject.getString("IdChamado"); 
       String solicitante = jobject.getString("Solicitante"); 
       Log.i("**id**", idChamado); 
       Log.i("**solicitante**", solicitante); 
      } 
     }catch(JSONException e) { 
      e.printStackTrace(); 
     }// END CATCH 
    }// END POST EXECUTE 
+0

向我們展示你迄今爲止的所作所爲。 –

+0

看看你試過了嗎? – TruongHieu

+0

http://stackoverflow.com/questions/26117313/android-how-to-parse-json-array-of-array-of-objects 可能重複。 –

回答

0
JSONObject jsonobject = new JSONObject("your JSON String here"); 
JSONArray jsonarray = jsonobject.getJSONArray("dados"); 

for (int i = 0; i < jsonarray.length(); i++) { 
    JSONObject jsonobject = jsonarray.getJSONObject(i); 
    String IdChamado = jsonobject.getString("IdChamado");  
    String Solicitante = jsonobject.getString("Solicitante"); 
} 

請儘量將

+0

隨時問,如果這不起作用:) :) –

+0

謝謝老,它的工作,但在一個循環。我把所有的值重複了4次或5次。任何明顯的原因,或者可能是我的代碼? – BlitzkriegBlue

+0

可以請你發佈你的編輯代碼嗎?將幫助你不用擔心@ArthurAbreu –

0

試試這個:

try{ 
    JSONObject json = new JSONObject(jsonString); 
    JSONArray jsonArray = json.getJSONArray("dados"); 
    for(int i=0;i<jsonArray.length();i++){ 
     JSONObject object = jsonArray.getJSONObject(i); 
     String IdChamado = object.getString("IdChamado"); 
     String Chamado = object.getString("Chamado"); 
     //rest of the strings.. 
     } 
    } 
    catch (JSONException e){ 
      e.printStackTrace(); 
    } 
+0

謝謝,它工作像扎基答案,但在一個循環。我把所有的值重複了4次或5次。任何明顯的原因,或者可能是我的代碼? – BlitzkriegBlue

+0

@ArthurAbreu你用日誌檢查過嗎? –

+0

感謝您的幫助,循環是我的錯誤。 – BlitzkriegBlue

0

試試這個

for (int i=0; i<jArray.length(); i++) { 
         JSONObject jsonobject = jArray.getJSONObject(i); 
         int IdChamado = jsonobject.getInt("IdChamado"); //idchamado here 
         String chamado = jsonobject.getString("Chamado"); 
         String solicitante = jsonobject.getString("Solicitante"); 

        } 
+0

感謝您的幫助,我很感激。 – BlitzkriegBlue

0

使用正確的鍵而選擇加入任何數據

@Override 
protected void onPostExecute(String result) { 
super.onPostExecute(result); 
try { 
JSONObject jsonObject = new JSONObject(result); 
JSONArray jArray = jsonObject.getJSONArray("dados"); 
Log.i("***2nd JSON ITSELF***", result); 
for (int i=0; i<jArray.length(); i++) { 
JSONObject jsonObject = new JSONObject(result); 
JSONArray jArray = jsonObject.optJSONArray("dados"); 
Log.i("***2nd JSON ITSELF***", result); 
for (int i=0; i<jArray.length(); i++) { 
JSONObject jsonPart = jArray.optJSONObject(i); 
int id = jsonPart.optInt("Id"); 
Log.i("***id***", String.valueOf(id)); 
String chamado = jsonPart.optString("Chamado"); 
Log.i("***Chamado***", chamado); 
String solicitante = jsonPart.optString("Solicitante"); 
Log.i("***Solicitante***", solicitante); 
String itemDeCatalogo = jsonPart.optString("ItemDeCatalogo"); 
Log.i("***Item de Catalogo***", itemDeCatalogo); 
} 
}catch(JSONException e) { 
e.printStackTrace(); 
}// END CATCH 
} 
0

我寫了一個庫,用於分析和Android的 http://github.com/amirdew/JSON

產生JSON爲您的樣品:

JSON jsonData = new JSON(jsonString); 

//access IdChamado of first item: 
int IdChamado = jsonData.key("dados").index(0).key("IdChamado").intValue(); 


//in loop: 

JSON dadosList = jsonData.key("dados"); 

for(int i=0; i<dadosList.count(); i++){ 
    int IdChamado = dadosList.index(i).key("IdChamado").intValue(); 
    String Chamado = dadosList.index(i).key("Chamado").stringValue(); 
}