访问嵌套的JSON Android [英] Access Nested JSON Android

查看:92
本文介绍了访问嵌套的JSON Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在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具有实际名称,例如

I need to access the values "IdChamado", "chamado", "Solicitante", for example. I've seen nested arrays answers but with jsonObjects having an actual name, like this .

PS:对不起,我忘了发布代码了:

PS: I'm sorry I forgot to post my codes:

 //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

[SOLVED]: Thank you so much people, you are the reason I like to code (Not be afraid of asking stupid questions). It all worked well with the codes you sent as answer. I thought it would be more complicated. 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

推荐答案

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

请尝试这个

这篇关于访问嵌套的JSON Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆