2014-05-05 90 views
0
$(document).ready(function() { 
    $.ajax({ 
     type: 'POST', 
     url: 'http:abc/public/aditya/eJewelry/get_store_details.php?store_id=udb1001', 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (data) { 
      alert(data); 
     } 
    }); 
}); 

我發送請求到該網址並獲得性反應: -jQuery的Ajax和JSON解析問題

[{ 
    "id": "22", 
    "storename": "Unique Diamond Boutique", 
    "email": "[email protected]", 
    "storeimage_ipad": "", 
    "website": "www.uniquediamondboutique.com", 
    "store_code": "udb1001", 
    "street_address": "157 E New England Avenue\nSuite 202\n", 
    "country": "US", 
    "zipcode": "32789", 
    "state": "FL", 
    "city": "Winter Park", 
    "email_sales": "[email protected]", 
    "personal_phone": "407-312-6768", 
    "company_phone": "407-218-5958", 
    "image": "store_22.png", 
    "status": "1", 
    "paypal_email": "[email protected]", 
    "moreAddress": [{ 
     "street_address": "123 main st", 
     "city": "MIAMI", 
     "state": "FL", 
     "zipcode": "33106", 
     "country": "", 
     "website": "", 
     "id": "68", 
     "company_phone": "", 
     "company_email": "" 
    }, { 
     "street_address": "640 Brevard Ave", 
     "city": "Cocoa Beach", 
     "state": "FL", 
     "zipcode": "32922", 
     "country": "", 
     "website": " ", 
     "id": "69", 
     "company_phone": "407-123-5678", 
     "company_email": " " 
    }] 
}] 

當我使用alert(數據)我得到「的翻譯:」;

我如何使用該數據來獲取ID,國家,email_sales..etc。

我使用alert(data.id),但得到空警報。如果您有任何想法,請讓我知道。

+1

它是一個對象數組,因此數據[0] .id –

+0

如果我找到了你,你想記錄所有的屬性,而不僅僅是ID,對吧?我添加了一個答案,列出了數據對象的所有屬性。 – Matt

回答

1

在你的成功的功能使用$.each()得到的對象:

success: function (data) { 
    $.each(data, function(i, item){ // get in the array to have objects 
     console.log(item.id); // logs 22, 
     $.each(item.moreAddress, function(_, resp){ // here get one more level deep 
      console.log(resp.id); // logs 68, 69 
     }); 
    }); 
} 
0

嘗試

alert(data[0].id); 

希望這有助於

5

這是因爲data是對象的數組,其默認toString()實現返回[object Object]

要查看對象的值而不是alert()請使用console.log()developer console)。

由於數據是一個數組,因此您需要遍歷它以獲取類似的值。由於您使用的是jQuery,因此您可以使用$.each()來完成此操作(否則,您可以使用正常的循環語句或Array.forEach())。

$.each(data, function(i, item){ 
    console.log(item.id) 
}) 
+1

我確定@Arun在*之前回答了這些問題! :-) –

0

我看到到目前爲止都只是給你回的ID的價值,而不是每個屬性的答案。 試試這個,這將打印對象的項目到控制檯:

function listObject(obj) { 
    $.each(obj, function(i, item1){ 
     console.log('obj['+i+'] contents:'); 
     $.each(item1, function(j, item2){ 
       console.log(j+'='+JSON.stringify(item2)) 
     });  
    }); 
}; 

listObject(data); 

出於演示,我創建了一個Fiddle demo。 它將輸出(按F12Internet Explorer中獲得控制檯輸出 - 然後單擊控制檯選項卡):

LOG: obj[0] contents: 
LOG: id="22" 
LOG: storename="Unique Diamond Boutique" 
LOG: email="[email protected]" 
LOG: storeimage_ipad="" 
LOG: website="www.uniquediamondboutique.com" 
LOG: store_code="udb1001" 
LOG: street_address="157 E New England Avenue\nSuite 202\n" 
LOG: country="US" 
LOG: zipcode="32789" 
LOG: state="FL" 
LOG: city="Winter Park" 
LOG: email_sales="[email protected]" 
LOG: personal_phone="407-312-6768" 
LOG: company_phone="407-218-5958" 
LOG: image="store_22.png" 
LOG: status="1" 
LOG: paypal_email="[email protected]" 
LOG: moreAddress=[{"street_address":"123 main st","city":"MIAMI","state":"FL","zipcode":"33106","country":"","website":"","id":"68","company_phone":"","company_email":""}, 
     {"street_address":"640 Brevard Ave","city":"Cocoa Beach","state":"FL","zipcode":"32922","country":"","website":" ","id":"69","company_phone":"407-123-5678","company_email":" "} 
     ] 

您也可以使函數的遞歸遍歷整個對象,但我認爲,這種方法對於你所需要的很簡單。