使用jQuery循环使用JSON结构

时间:2016-08-25 10:14:34

标签: javascript jquery json

我在通过jQuery循环JSON结构时遇到了一些麻烦,

这是我的JSON数据:

  {
    "suppliers": [
        {
            "Supplier": {
                "id": "5704ebeb-e5e0-4779-aef4-16210a00020f",
                "name": "Gillmans",
                "mobile": "",
                "office_telephone": "00000",
                "ooh_contact": "00000",
                "fax_number": "",
                "address_line_1": "St Oswalds Road",
                "address_line_2": "Gloucester",
                "address_line_3": "",
                "address_line_4": "",
                "postcode": "GL1 2SG",
                "email": "email@example.com",
                "contact": "",
                "position": "",
                "aov": "180.00",
                "engineer": false,
                "cc_on_new_job_emails": true,
                "can_add_quotes": false,
                "notes": "",
                "status": "1",
                "created": "2016-04-06 11:58:51",
                "modified": "2016-07-27 11:23:01",
                "status_text": "Active",
                "engineer_text": "No",
                "cc_on_new_job_emails_text": "Yes"
            },
            "Trade": [],
            "PostcodeArea": []
        },
        {
            "Supplier": {
                "id": "571e390f-91e8-4745-8f78-168b0a00020f",
                "name": "Kings",
                "mobile": "",
                "office_telephone": "00000",
                "ooh_contact": "0000",
                "fax_number": "",
                "address_line_1": "",
                "address_line_2": "",
                "address_line_3": "",
                "address_line_4": "",
                "postcode": "",
                "email": "",
                "contact": "",
                "position": "Account Manager; Joanne Brook",
                "aov": null,
                "engineer": false,
                "cc_on_new_job_emails": false,
                "can_add_quotes": false,
                "notes": "",
                "status": "1",
                "created": "2016-04-25 16:34:39",
                "modified": "2016-07-08 15:22:15",
                "status_text": "Active",
                "engineer_text": "No",
                "cc_on_new_job_emails_text": "No"
            },
            "Trade": [],
            "PostcodeArea": []
        }
]
}

这个JSON是从我的AJAX调用中返回的一个名为data的变量。 data是一个Javascript对象,即它已经被ajax调用解析了。

我正在尝试遍历此JSON数据并获取nameid属性。我是这样做的:

  $.each(data, function(k, v) {    
            $.each(this, function(key, val) {
                $.each(this, function(key2, val2) {
                    $.each(this, function(key3, val3) {
                    if(key3 == 'name')
                    {
                    alert(val3);
                    }

                      });
                   });
                });
            });

这将打印所有name值,但显然这是一个非常混乱的方式,我想知道是否有一种更简单的方法可以获得nameid属性这个结构并将它们存储在变量中?

3 个答案:

答案 0 :(得分:5)

您可以处理JSON as an object if you parse it

//this has already been done by the ajax call by the sounds of it.
//var jsObj = JSON.parse(data);

//suppliers is an array now ([]), so loop it
$.each(data.suppliers, function(index, value){
    //value is a supplier object ({}) so you can acces it's properties directly
    alert(value.Supplier.name);
});

答案 1 :(得分:4)

$.each(data.suppliers, function(){
    alert(this.Supplier.id);
});

答案 2 :(得分:0)

试试这个:

var data = {
           "suppliers":[
              {
                 "Supplier":{
                    "id":"5704ebeb-e5e0-4779-aef4-16210a00020f",
                    "name":"Gillmans"
                 },
                 "Trade":[

                 ],
                 "PostcodeArea":[

                 ]
              },
              {
                 "Supplier":{
                    "id":"571e390f-91e8-4745-8f78-168b0a00020f",
                    "name":"Kings"
                 },
                 "Trade":[

                 ],
                 "PostcodeArea":[

                 ]
              }
           ]
        }

     $.each(data.suppliers, function(k, v) {    
           alert(this.Supplier.id);
       })