Skip to content Skip to sidebar Skip to footer

Extract From Ajax Response

I have an ajax call that returns JSON data (Data Attached). After converting the data into String, this is how it looks like: { 'shipments':[ { 'companyName':'Tr

Solution 1:

if you get a string that is json, you need to parse it first.

var obj = JSON.parse(jsonString);

now you have a proper object literal, and you can access it normally

var shipments = obj.shipments;

shipments is now a javascript array...

for(var i = 0; i < shipments.length; i++){
    console.log(shipments[i].companyName);
}

note you should not use the for(var i in x) construct on arrays.

Solution 2:

response.shipments[i].companyName

Post a Comment for "Extract From Ajax Response"