StealthRT
StealthRT

Reputation: 10542

Parsing JSON string via Jquery Ajax response

Hey all Here is my code:

function SuccessOccur(data, status, req) {
    if (status == "success") {
       var xml = req.responseText.toString();
       console.log(xml);
       var jSON = $(xml).find('empResult').text();
       var obj = jQuery.parseJSON(jSON);
       console.log(obj.firstName);
    }
}

Oddly enough I get this as a value:

undefined

If I just put this:

console.log(obj);

Then I would get all the values:

enter image description here

So, what am I missing?

Upvotes: 0

Views: 463

Answers (1)

adeneo
adeneo

Reputation: 318162

obj is not an object, it's an array, that's why it's in [] brackets with a 0 as the key, and a given length of 1 in the console output you've posted. Arrays are accessed like this

console.log(obj[0].firstName);

Upvotes: 5

Related Questions