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

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.

2
  • 1
    It's an array of objects so data[0].id Commented May 5, 2014 at 12:46
  • If I got you right, you wanted to log all properties, not just the ID, right? I've added an answer which lists all properties of the data object. Commented May 5, 2014 at 14:52

4 Answers 4

5

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)
})
Sign up to request clarification or add additional context in comments.

1 Comment

I'm sure @Arun answers these before they're asked! :-)
1

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

Comments

0

Try

alert(data[0].id);

Hope this help

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.