JSONP error: Cannot read property '0' of undefined

I have some JSON data that I am trying to parse through Javascript and I keep receiving a Cannot read property ‘0’ of undefined error.

This is a short snippet of the data that comes in: “shippingInfo”:[{“shippingServiceCost”:[{“@currencyId”:“USD”,“value”:“1.5”}]

As I loop through my JSON results, I use this code to grab the shippingInfo data:

var shipping = parseFloat(items.shippingInfo[0].shippingServiceCost[0].value);

However, it appears each time that I receive and error on the shippingServiceCost[0] array, saying its undefined.

Any ideas?

I tested with the following code and had the alert displaying 1.5 as expected.

var items = '"shippingInfo":[{"shippingServiceCost":[{"@currencyId":"USD","__value__":"1.5"}]}]';
items = JSON.parse('{'+items+'}');
alert(items.shippingInfo[0].shippingServiceCost[0].__value__);

I had to add the extra }] to the end to close the data - I assume that you have further values following in the actual string and that those two characters are at the end.

I am not really sure why you can’t get your code to work - there isn’t exactly all that much code required.

I am not sure if this makes any difference, but this is how I loop through the items:


$.each(data.findItemsByKeywordsResponse[0].searchResult[0].item, function(i,items){
								
	var shipping = parseFloat(items.shippingInfo[0].shippingServiceCost[0].__value__);

});

Using JQuery 1.7