Sun Love
Sun Love

Reputation: 752

Jquery ajax with json parsing issue

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

I am sending request to that url and getting responce:-

[{
    "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": " "
    }]
}]

When i use alert(data) i am getting "[object Object]";

How can i use that data to get the id, country,email_sales..etc.

I am using alert(data.id) but getting empty alert. If you have any idea kindly let me know.

Upvotes: 0

Views: 80

Answers (4)

Matt
Matt

Reputation: 27026

The answers I have seen so far are just giving you back the value of the ID, not of every property. Try this, it will print the object items to the console:

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

For demonstration, I have created a Fiddle demo. It will output (press F12 in Internet Explorer to get console output - then click the console tab):

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":" "}
        ] 

You can also make the function recursive to traverse the entire object but I think this approach is simple enough for what you need.

Upvotes: 0

Jai
Jai

Reputation: 74738

In your success function use $.each() to get the objects:

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

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388436

It is because data is an array of objects, whose default toString() implementation returns [object Object].

To see the value of a object instead of alert() use console.log()(developer console).

Since data is an array, you need to iterate through it to get the values like. Since you are using jQuery you can use $.each() to do that(you can otherwise use a normal loop statement or Array.forEach()).

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

Upvotes: 5

harsh4u
harsh4u

Reputation: 2600

Try

alert(data[0].id);

Hope this help

Upvotes: 0

Related Questions