2016-08-25 93 views
2

我有穿過的jQuery的JSON結構循環一些麻煩循環,通過JSON結構與jQuery

這裏是我的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 protected]", 
       "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調用的返回變量叫做datadata是一個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性質這個結構並將它們存儲在變量中?

回答

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

這一個似乎爲我做,下面的答案拋出一個錯誤。 – user3574492

+0

所以它不是JSON。 JSON!== JavaScript對象 – Liam

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); 
}); 
+0

在控制檯拋出Error'57bc12cc-4d4c-4063-9b3c-54369a44812a:1未捕獲的SyntaxError :JSON在位置1的意外標記o – user3574492

+0

您沒有說明數據是字符串還是對象?這個錯誤似乎暗示它是一個對象,因此根本不是JSON。 [JavaScript對象Vs JSON](http://stackoverflow.com/questions/8294088/javascript-object-vs-json) – Liam

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