I'm trying to get data from a JSON array within a JSON array, but am having little success.
So our server returns a JSON array within a variable called 'msg', which look like this when I console.log it
[{"product":"[{\"imgUrl\":\"/p/332401\",\"prodCartQty\":2,\"prodName\":\"Test Product 1",\"productCode\":\"332401\"},{\"imgUrl\":\"/p/332388\",\"prodCartQty\":2,\"prodName\":\"Test Product 2",\"productCode\":\"332388\"}]","category":"gross_MAX_1","totalCategory":1},{"product":"[{\"imgUrl\":\"/p/332401\",\"prodCartQty\":2,\"prodName\":\"Test Product 1",\"productCode\":\"332401\"},{\"imgUrl\":\"/p/332388\",\"prodCartQty\":2,\"prodName\":\"Test Product 2",\"productCode\":\"332388\"}]","category":"gross_MAX_1","totalCategory":1}]
I then have the following code to pull out the product name but keep getting an undefined error - Uncaught TypeError: Cannot read property 'prodName' of undefined
console.log(msg);
var data = $.parseJSON(msg);
console.log(data);
OBJ = {};
OBJ.apply = function(data) {
$.each(data, function() {
$.each(this, function(key, value) {
console.log(value.product.prodName);
});
});
};
OBJ.apply(data);
Any ideas where i'm going wrong?
Thanks
EDIT
I've looked at the parsed variable and I get:-
[Object, Object]
0: Object
category: "restrictedItems_ie-standard-gross_MAX_1"
product: "[{"imgUrl":"/p/332401","prodCartQty":2,"prodName":"Test Product 1","productCode":"332401"},{"imgUrl":"/p/332388","prodCartQty":2,"prodName":"Test Product 2","productCode":"332388"}]"
totalCategory: 1
__proto__: Object
1: Object
category: "restrictedItems_ie-standard-gross_MAX_1"
product: "[{"imgUrl":"/p/332401","prodCartQty":2,"prodName":"Test Product 1","productCode":"332401"},{"imgUrl":"/p/332388","prodCartQty":2,"prodName":"Test Product 2","productCode":"332388"}]"
totalCategory: 1
__proto__: Object
length: 2
__proto__: Array[0]
So the data parses fine but now I need to return the data in product or get the object data in Product into a separate object. Any ideas? thanks