访问嵌套的JSON Android

问题描述 投票:0回答:5

我想知道我如何在Android中访问此JSON的值:

{
    "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
android json
5个回答
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
投票

尝试一下:

 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
投票

尝试一下

    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
投票

选择任何数据时使用正确的键

@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中解析和生成JSON的库http://github.com/amirdew/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();
}
© www.soinside.com 2019 - 2024. All rights reserved.