如何在android上使用Json解析嵌套数组

时间:2014-07-25 20:02:50

标签: android arrays json parsing

我现在正试图只获得" dados" > "诺姆"及其" contatos"> "诺姆"但是我解析的方式我得到了以下结果:

07-25 16:58:38.637    1041-1041/com.javatechig.feedreader I/System.out﹕ ClientesContatosModel{id=0, nome='Bill Gates', telefone=[], email=[]}
07-25 16:58:38.637    1041-1041/com.javatechig.feedreader I/System.out﹕ ClientesContatosModel{id=0, nome='Linus Torvalds', telefone=[], email=[]}
07-25 16:58:38.637    1041-1041/com.javatechig.feedreader I/System.out﹕ ClientesContatosModel{id=0, nome='Bill Gates', telefone=[], email=[]}
07-25 16:58:38.637    1041-1041/com.javatechig.feedreader I/System.out﹕ ClientesContatosModel{id=0, nome='Linus Torvalds', telefone=[], email=[]}
07-25 16:58:38.637    1041-1041/com.javatechig.feedreader I/System.out﹕ ClientesContatosModel{id=0, nome='Bill Gates', telefone=[], email=[]}
07-25 16:58:38.637    1041-1041/com.javatechig.feedreader I/System.out﹕ ClientesContatosModel{id=0, nome='Linus Torvalds', telefone=[], email=[]}
07-25 16:58:38.637    1041-1041/com.javatechig.feedreader I/System.out﹕ ClientesContatosModel{id=0, nome='Steve Jobs', telefone=[], email=[]}

" contatos"名字重复,直到姓氏"史蒂夫乔布斯"。 为什么要这样做,我该如何解决呢?

这是我完整的Json:

{
    "total": 4,
    "dados": [
        {
            "id": "9",
            "id_rm_empresa": "2",
            "id_rm_tipo_cliente": "1",
            "tipo_pessoa": "PJ",
            "cpf": null,
            "nome": "Microsoft",
            "cnpj": null,
            "ie": null,
            "suframa": null,
            "ramo_de_atividade": null,
            "ano_de_fundacao": null,
            "informacoes_adicionais": "informação do clientes",
            "enderecos": [
                {
                    "id_rm_endereco": "1",
                    "endereco": "Rua Rudolph Diesel",
                    "numero": "174",
                    "complemento": "",
                    "bairro": "Industrial",
                    "cep": "86063-330",
                    "cidade": "Londrina",
                    "estado": "Paraná",
                    "tipo_endereco": "Residencial"
                }
            ],
            "contatos": [
                {
                    "id_rm_cliente": "10",
                    "id_rm_contato": "1",
                    "id_rm_empresa": "2",
                    "nome": "Bill Gates",
                    "cargo": "Diretor",
                    "telefones": [
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_telefone": "4",
                            "id_rm_empresa": "2",
                            "tipo": "Trabalho",
                            "numero": "(22) 2222-2222",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        },
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_telefone": "9",
                            "id_rm_empresa": "2",
                            "tipo": "Celular",
                            "numero": "(44) 4444-4444",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        }
                    ],
                    "emails": [
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_email": "5",
                            "id_rm_empresa": "2",
                            "tipo": "Trabalho",
                            "email": "steve@apple-teste.com",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        }
                    ],
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                },
                {
                    "id_rm_cliente": "10",
                    "id_rm_contato": "7",
                    "id_rm_empresa": "2",
                    "nome": "Linus Torvalds",
                    "cargo": "Diretor",
                    "telefones": [
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_telefone": "4",
                            "id_rm_empresa": "2",
                            "tipo": "Trabalho",
                            "numero": "(22) 2222-2222",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        }
                    ],
                    "emails": [
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_email": "5",
                            "id_rm_empresa": "2",
                            "tipo": "Trabalho",
                            "email": "steve@apple-teste.com",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        }
                    ],
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                }
            ],
            "telefones": [
                {
                    "id_rm_empresa": "2",
                    "id_rm_cliente": "10",
                    "id_rm_telefone": "1",
                    "numero": "(43)3025-1355",
                    "tipo": "Casa",
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                },
                {
                    "id_rm_empresa": "2",
                    "id_rm_cliente": "10",
                    "id_rm_telefone": "2",
                    "numero": "(43)3000-6562",
                    "tipo": "Trabalho",
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                }
            ],
            "emails": [],
            "criado": "2013-09-01 07:00:00",
            "modificado": "2012-09-04 18:30:34",
            "status": "A"
        },
        {
            "id": "8",
            "id_rm_empresa": "2",
            "id_rm_tipo_cliente": "1",
            "tipo_pessoa": "PJ",
            "cpf": null,
            "nome": "Linux",
            "cnpj": null,
            "ie": null,
            "suframa": null,
            "ramo_de_atividade": null,
            "ano_de_fundacao": null,
            "informacoes_adicionais": "informação do clientes",
            "enderecos": [
                {
                    "id_rm_endereco": "1",
                    "endereco": "Rua Rudolph Diesel",
                    "numero": "174",
                    "complemento": "",
                    "bairro": "Industrial",
                    "cep": "86063-330",
                    "cidade": "Londrina",
                    "estado": "Paraná",
                    "tipo_endereco": "Residencial"
                }
            ],
            "contatos": [
                {
                    "id_rm_cliente": "10",
                    "id_rm_contato": "1",
                    "id_rm_empresa": "2",
                    "nome": "Bill Gates",
                    "cargo": "Diretor",
                    "telefones": [
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_telefone": "4",
                            "id_rm_empresa": "2",
                            "tipo": "Trabalho",
                            "numero": "(22) 2222-2222",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        },
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_telefone": "9",
                            "id_rm_empresa": "2",
                            "tipo": "Celular",
                            "numero": "(44) 4444-4444",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        }
                    ],
                    "emails": [
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_email": "5",
                            "id_rm_empresa": "2",
                            "tipo": "Trabalho",
                            "email": "steve@apple-teste.com",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        }
                    ],
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                },
                {
                    "id_rm_cliente": "10",
                    "id_rm_contato": "7",
                    "id_rm_empresa": "2",
                    "nome": "Linus Torvalds",
                    "cargo": "Diretor",
                    "telefones": [
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_telefone": "4",
                            "id_rm_empresa": "2",
                            "tipo": "Trabalho",
                            "numero": "(22) 2222-2222",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        }
                    ],
                    "emails": [
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_email": "5",
                            "id_rm_empresa": "2",
                            "tipo": "Trabalho",
                            "email": "steve@apple-teste.com",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        }
                    ],
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                }
            ],
            "telefones": [
                {
                    "id_rm_empresa": "2",
                    "id_rm_cliente": "10",
                    "id_rm_telefone": "1",
                    "numero": "(43)3025-1355",
                    "tipo": "Casa",
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                },
                {
                    "id_rm_empresa": "2",
                    "id_rm_cliente": "10",
                    "id_rm_telefone": "2",
                    "numero": "(43)3000-6562",
                    "tipo": "Trabalho",
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                }
            ],
            "emails": [],
            "criado": "2013-09-01 07:00:00",
            "modificado": "2012-09-04 18:30:34",
            "status": "A"
        },
        {
            "id": "19",
            "id_rm_empresa": "2",
            "id_rm_tipo_cliente": "1",
            "tipo_pessoa": "PJ",
            "cpf": null,
            "nome": "Apple",
            "cnpj": null,
            "ie": null,
            "suframa": null,
            "ramo_de_atividade": null,
            "ano_de_fundacao": null,
            "informacoes_adicionais": "informação do clientes",
            "enderecos": [
                {
                    "id_rm_endereco": "1",
                    "endereco": "Rua Rudolph Diesel",
                    "numero": "174",
                    "complemento": "",
                    "bairro": "Industrial",
                    "cep": "86063-330",
                    "cidade": "Londrina",
                    "estado": "Paraná",
                    "tipo_endereco": "Residencial"
                }
            ],
            "contatos": [
                {
                    "id_rm_cliente": "10",
                    "id_rm_contato": "1",
                    "id_rm_empresa": "2",
                    "nome": "Bill Gates",
                    "cargo": "Diretor",
                    "telefones": [
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_telefone": "4",
                            "id_rm_empresa": "2",
                            "tipo": "Trabalho",
                            "numero": "(22) 2222-2222",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        },
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_telefone": "9",
                            "id_rm_empresa": "2",
                            "tipo": "Celular",
                            "numero": "(44) 4444-4444",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        }
                    ],
                    "emails": [
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_email": "5",
                            "id_rm_empresa": "2",
                            "tipo": "Trabalho",
                            "email": "steve@apple-teste.com",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        }
                    ],
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                },
                {
                    "id_rm_cliente": "10",
                    "id_rm_contato": "7",
                    "id_rm_empresa": "2",
                    "nome": "Linus Torvalds",
                    "cargo": "Diretor",
                    "telefones": [
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_telefone": "4",
                            "id_rm_empresa": "2",
                            "tipo": "Trabalho",
                            "numero": "(22) 2222-2222",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        }
                    ],
                    "emails": [
                        {
                            "id_rm_cliente": "10",
                            "id_rm_contato": "1",
                            "id_rm_email": "5",
                            "id_rm_empresa": "2",
                            "tipo": "Trabalho",
                            "email": "steve@apple-teste.com",
                            "criado": "2013-09-01 07:00:00",
                            "modificado": "2013-09-03 18:30:30",
                            "status": "A"
                        }
                    ],
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                }
            ],
            "telefones": [
                {
                    "id_rm_empresa": "2",
                    "id_rm_cliente": "10",
                    "id_rm_telefone": "1",
                    "numero": "(43)3025-1355",
                    "tipo": "Casa",
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                },
                {
                    "id_rm_empresa": "2",
                    "id_rm_cliente": "10",
                    "id_rm_telefone": "2",
                    "numero": "(43)3000-6562",
                    "tipo": "Trabalho",
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                }
            ],
            "emails": [],
            "criado": "2013-09-01 07:00:00",
            "modificado": "2012-09-04 18:30:34",
            "status": "A"
        },
        {
            "id": "10",
            "id_rm_empresa": "2",
            "id_rm_tipo_cliente": "1",
            "tipo_pessoa": "PJ",
            "cpf": null,
            "nome": "Webee",
            "cnpj": null,
            "ie": null,
            "suframa": null,
            "ramo_de_atividade": null,
            "ano_de_fundacao": null,
            "informacoes_adicionais": "informação do clientes",
            "enderecos": [
                {
                    "id_rm_endereco": "1",
                    "endereco": "Rua Rudolph Diesel",
                    "numero": "174",
                    "complemento": "",
                    "bairro": "Industrial",
                    "cep": "86063-330",
                    "cidade": "Londrina",
                    "estado": "Paraná",
                    "tipo_endereco": "Residencial"
                }
            ],
            "contatos": [
                {
                    "id_rm_cliente": "10",
                    "id_rm_contato": "1",
                    "id_rm_empresa": "2",
                    "nome": "Steve Jobs",
                    "cargo": "Diretor",
                    "telefones": {
                        "total": 1,
                        "dados": [
                            {
                                "id_rm_cliente": "10",
                                "id_rm_contato": "1",
                                "id_rm_telefone": "4",
                                "id_rm_empresa": "2",
                                "tipo": "Trabalho",
                                "numero": "(22) 2222-2222",
                                "criado": "2013-09-01 07:00:00",
                                "modificado": "2013-09-03 18:30:30",
                                "status": "A"
                            }
                        ]
                    },
                    "emails": {
                        "total": 1,
                        "dados": [
                            {
                                "id_rm_cliente": "10",
                                "id_rm_contato": "1",
                                "id_rm_email": "5",
                                "id_rm_empresa": "2",
                                "tipo": "Trabalho",
                                "email": "steve@apple-teste.com",
                                "criado": "2013-09-01 07:00:00",
                                "modificado": "2013-09-03 18:30:30",
                                "status": "A"
                            }
                        ]
                    },
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                }
            ],
            "telefones": [
                {
                    "id_rm_empresa": "2",
                    "id_rm_cliente": "10",
                    "id_rm_telefone": "1",
                    "numero": "(43)3025-1355",
                    "tipo": "Casa",
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                },
                {
                    "id_rm_empresa": "2",
                    "id_rm_cliente": "10",
                    "id_rm_telefone": "2",
                    "numero": "(43)3000-6562",
                    "tipo": "Trabalho",
                    "criado": "2013-09-01 07:00:00",
                    "modificado": "2013-09-03 18:30:30",
                    "status": "A"
                }
            ],
            "emails": [],
            "criado": "2013-09-01 07:00:00",
            "modificado": "2013-09-03 18:30:30",
            "status": "A"
        }
    ]
}

这是我到目前为止所尝试的内容:

contatoList = new ArrayList<ClientesContatosModel>();
    JSONArray dados = json.getJSONArray("dados");
    feedList = new ArrayList<ClientesModel>();

    // parsing json object
    for (int i = 0; i < dados.length(); i++) {

        JSONObject item = dados.getJSONObject(i);


        ClientesModel mClientesModel = new ClientesModel();


        /* cadastra os dados necessários no objeto no modelo */

        mClientesModel.setNome(item.optString("nome"));
        mClientesModel.setTipo_pessoa(item.optString("tipo_pessoa"));
        mClientesModel.setInformacoes_adicionais(item.optString("informacoes_adicionais"));
        mClientesModel.setCpf(item.optString("cpf"));
        mClientesModel.setCnpj(item.optString("cnpj"));
        JSONArray contatos = item.getJSONArray("contatos");


            for (int j = 0; j < contatos.length(); j++) {

                ClientesContatosModel mClientesContatoModel = new ClientesContatosModel();

                JSONObject data = contatos.getJSONObject(j);

                mClientesContatoModel.setNome(data.optString("nome"));


                mClientesContatoModel.setCargo(data.optString("cargo"));

                contatoList.add(mClientesContatoModel);

            }


            feedList.add(mClientesModel);

            System.out.println(contatoList);


    }

2 个答案:

答案 0 :(得分:1)

移动

ClientesContatosModel mClientesContatoModel = new ClientesContatosModel();

contatoList.add(mClientesContatoModel);

里面

for (int j = 0; j < contatos.length(); j++) {

示例:

for (int j = 0; j < contatos.length(); j++) {
 ClientesContatosModel mClientesContatoModel = new ClientesContatosModel();
 contatoList.add(mClientesContatoModel);

答案 1 :(得分:1)

这行代码:

ClientesContatosModel mClientesContatoModel = new ClientesContatosModel();

应该在它所属的FOR里面。

for (int j = 0; j < contatos.length(); j++) {
                // HERE
                ClientesContatosModel mClientesContatoModel = new ClientesContatosModel();
                JSONObject data = contatos.getJSONObject(j);
                mClientesContatoModel.setNome(data.optString("nome"));
                mClientesContatoModel.setCargo(data.optString("cargo"));
            }