I have an ajax call that returns the following JSON:
returnedData = "[
{ id: 1, firstName: 'John', lastName: 'Smith', address: '123 Spa Road', city: 'London',
orders:
[
{ product: 'TV', price: 599.99, quantity: 2, orderTotal: 1199.98 }
]
}
]";
var customers = JSON.parse (returnedData);
console.log(customers.length); // prints length of the string data
It treats it as string. However, it I assign the result directly.
var customers = [
{ id: 1, firstName: 'John', lastName: 'Smith', address: '123 Spa Road', city: 'London',
orders:
[
{ product: 'TV', price: 599.99, quantity: 2, orderTotal: 1199.98 }
]
}
];
console.log(customers.length); // prints 1 - the number of objects
Why is it like this? How can I assign it dynamically?
prints length as string[{"id":1,"firstName":"John","lastName":"Smith","address":"123 Spa Road","city":"London","orders":[{"product":"TV","price":599.99,"quantity":2,"orderTotal":1199.98}]}]is valid JSON data.