Jquery ajax与json解析问题

Sun*_*ove 0 ajax jquery json

$(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);
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

我正在向该网址发送请求并获得回复: -

[{
    "id": "22",
    "storename": "Unique Diamond Boutique",
    "email": "croberts@uniquediamondboutique.com",
    "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": "croberts@parkavegold.com",
    "personal_phone": "407-312-6768",
    "company_phone": "407-218-5958",
    "image": "store_22.png",
    "status": "1",
    "paypal_email": "croberts@uniquediamondboutique.com",
    "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": " "
    }]
}]
Run Code Online (Sandbox Code Playgroud)

当我使用警报(数据)时,我得到"[object Object]";

我如何使用该数据来获取id,country,email_sales..etc.

我正在使用alert(data.id)但获得空警报.如果您有任何想法,请告诉我.

Aru*_*hny 5

这是因为data是一个对象数组,其默认toString()实现返回 [object Object].

要查看对象的值而不是alert()使用console.log()(开发人员控制台).

由于数据是一个数组,您需要遍历它以获取类似的值.由于您使用的是jQuery,因此可以使用$ .each()来执行此操作(否则可以使用普通的循环语句或Array.forEach()).

$.each(data, function(i, item){
    console.log(item.id)
})
Run Code Online (Sandbox Code Playgroud)