获取json对象中的信息

时间:2014-12-15 14:17:42

标签: jquery json

我有这个json对象:

[
{
    "debut": "08-12-2014",
    "fin": "09-12-2014",
    "date_inconnue": "",
    "client_desactive": "0",
    "numero_info_fact": "0"
},
{
    "groupe_1": {
        "infos": {
            "commandes_id": "95",
            "groupes_id": "143",
            "lieu_depart": "Delta",
            "lieu_arrivee": "Brugges",
            "heure_depart": "9:00",
            "heure_arrivee": "13:00",
            "id": "143",
            "nom": "Nijs",
            "taille": "4"
        },
        "langues": [
            {
                "groupes_id": "143",
                "langues_id": "2",
                "id": "2",
                "langue": "Néerlandais",
                "statut": "0"
            }
        ],
        "langues_guide": [
            {
                "guides_id": "3",
                "langues_id": "1"
            },
            {
                "guides_id": "3",
                "langues_id": "2"
            },
            {
                "guides_id": "3",
                "langues_id": "3"
            }
        ],
        "activites": [],
        "fournisseurs": [
            {
                "groupes_id": "143",
                "fournisseurs_id": "15",
                "prix_achat": null,
                "prix_vente": null,
                "paye_par_bb": "1",
                "remarque": "",
                "id": "15",
                "nom": "Cars Dessaintes",
                "types_fournisseurs_id": "3",
                "tel": "02 355 89 87",
                "fax": "",
                "adresse": "Chaussée de Tubize",
                "cpostal": "1480",
                "commune": "Saintes",
                "fournisseurs_cat": [
                    {
                        "id": "15",
                        "nom": "Cars Dessaintes",
                        "types_fournisseurs_id": "3",
                        "tel": "02 355 89 87",
                        "fax": "",
                        "adresse": "Chaussée de Tubize",
                        "cpostal": "1480",
                        "commune": "Saintes"
                    },
                    {
                        "id": "1",
                        "nom": "Cars Rent",
                        "types_fournisseurs_id": "3",
                        "tel": "0474 74 74 74",
                        "fax": "0474 75 75 75",
                        "adresse": "Rue Lambin 52",
                        "cpostal": "1050",
                        "commune": "Ixelles"
                    }
                ]
            }
        ],
        "guides": [
            {
                "guides_id": "3",
                "nom": "Kiss",
                "prenom": "Robert",
                "remarque": "",
                "id": "756",
                "prestations": [
                    {
                        "id": "60",
                        "nom": "Brel",
                        "types_activites_id": "1",
                        "remarque": "Quel bon guide..."
                    }
                ]
            }
        ],
        "contacts": [
            {
                "id": "41",
                "remarque_contact_groupe": "Ne me quitte pas",
                "telephone_contact_groupe": "03 888 77 78",
                "mail_contact_groupe": null,
                "prenom_contact_groupe": null,
                "nom_contact_groupe": "Jacques Brel",
                "gsm_contact_groupe": null,
                "groupe_id": "143"
            },
            {
                "id": "42",
                "remarque_contact_groupe": "Oh mon Dieu",
                "telephone_contact_groupe": "02 888 78 98",
                "gsm_contact_groupe": null,
                "groupe_id": "143"
            }
        ]
    },
    "groupe_2": {
        "infos": {
            "commandes_id": "95",
            "groupes_id": "147",
            "lieu_depart": "RTL HOUSE",
            "lieu_arrivee": "",
            "id": "147",
            "nom": "RTL",
            "taille": ""
        },
        "langues": [
            {
                "groupes_id": "147",
                "langues_id": "3",
                "id": "3",
                "langue": "Anglais",
                "statut": "0"
            }
        ],
        "langues_guide": [
            {
                "guides_id": "1",
                "langues_id": "1"
            },
            {
                "guides_id": "1",
                "langues_id": "3"
            }
        ],
        "activites": [],
        "fournisseurs": [],
        "guides": [
            {
                "guides_id": "1",
                "nom": "Mets",
                "prenom": "Anthony",
                "remarque": "rthdgfhgfh dgdgdsgf",
                "id": "757",
                "prestations": [
                    {
                        "id": "127",
                        "nom": "Bourgmestres",
                        "types_activites_id": "1",
                        "remarque": ""
                    }
                ]
            }
        ],
        "contacts": [
            {
                "id": "58",
                "remarque_contact_groupe": "La plus belle !!!",
                "telephone_contact_groupe": "",
                "mail_contact_groupe": null,
                "prenom_contact_groupe": null,
                "nom_contact_groupe": "Pandeville Sophie",
                "gsm_contact_groupe": null,
                "groupe_id": "147"
            }
        ]
    }
},**
[
    {
        "id": "646",
        "id_commande": "95",
        "id_guide": "3",
        "heures_supplementaires": "0",
        "paiement_sur_place": "1",
        "remarque": "",
        "payements": [],
        "factures": [],
        "ardoises": []
    },
    {
        "id": "647",
        "id_commande": "95",
        "id_guide": "1",
        "heures_supplementaires": "0",
        "paiement_sur_place": "1",
        "remarque": "",
        "payements": [],
        "factures": [],
        "ardoises": []
    }
],
[
    [
        {
            "id": "1",
            "titre": "0",
            "nom": "Cornette",
            "prenom": "Arielle",
            "fonction": "",
            "nom_societe": "",
            "status_id": "2",
            "langues_id": "1",
            "tva": "",
            "client_id": "0",
            "client_desactive": "0",
            "remarque": "RAF",
            "numero_info_fact": "0",
            "langue": "Français",
            "statut": "0"
        },
        {
            "id": "1",
            "rue": "rue de l'Abattoir#Près de la Mosquée#Grand Duplex",
            "numero": "4",
            "boite": "5",
            "cp": "1070",
            "ville": "Anderlecht",
            "mail": "arcornet@ulb.ac.be",
            "telephone": "02 676 78 78",
            "gsm": "0467 76 78 90",
            "fax": "02 650 59 59",
            "types_informations_contact_id": "1",
            "clients_id": "60",
            "pays_id": "1",
            "pays": "Belgique"
        }
    ],
    [
        {
            "id": "7",
            "gsm_contact_client": "0467 76 78 90",
            "fax_contact_client": "02 650 59 59",
            "telephone_contact_client": "02 676 78 78",
            "mail_contact_client": "arcornet@ulb.ac.be",
            "prenom_contact_client": "Arielle",
            "nom_contact_client": "Cornette",
            "clients_id": "60",
            "remarque": ""
        },
        {
            "id": "8",
            "gsm_contact_client": "0478 76 78 78",
            "fax_contact_client": "",
            "telephone_contact_client": "02 786 78 78",
            "mail_contact_client": "alice.boutboul@gmail.com",
            "prenom_contact_client": "Alice",
            "nom_contact_client": "Boutboul",
            "clients_id": "60",
            "remarque": "BELLE-FILLE"
        }
    ]
]

我想获得这些信息:

来自

{
    "groupe_1": {
        "infos": {
to

"gsm_contact_groupe": null,
                "groupe_id": "147"

我尝试了几种方法,但我无法获得这部分json objet。

run_in_tree(retour[1]);

                function run_in_tree(obj)
                {
                   for (key in obj)
                   {

                         $("#info_groupes").after('Key => '+obj[key]);
                         $("#info_groupes").after("<br>");
                         if (typeof obj[key] == 'object') run_in_tree(obj[key]);
                   }
                }

我无法获取信息(密钥和值)

当我必须浏览一个大型的json对象时,我遇到了一些困难。

1 个答案:

答案 0 :(得分:0)

首先,看起来你有一个数组。其中该数组中的第二个对象似乎包含每个组的属性。

所以要迭代这些组,你会这样做:

var groups = obj[1];// Get the 2nd object;

for(var key in groups){
    var group = groups[key];
    console.log(group.infos.commandes_id);
}

然后,您可以使用相同的逻辑来迭代任何子对象的属性。例如,如果要记录所有infos数据:

for(var infoKey in group.infos){
    var info = group.infos[infoKey];
    console.log(infoKey + " = " + info);
}

并迭代contacts数组:

for(var i = 0; i < group.contacts.length; i++){
    var contact = group.contacts[i];
    console.log(contact.id);
}

当你使用像这样的大JSON时,它有助于记住数组[]和对象{}之间的区别,因为你想要以不同的方式访问它们(如上所示) )。

希望能帮助你到达目的地