处理我有一个AJAX调用返回以下JSON: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
它把它作为字符串。但是,我直接分配结果。
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
这是为什么?我如何动态分配它?
不知道你的意思'打印长度string' –
什么代码编写的方式,它不会返回任何东西,但一个错误。 “未捕获的SyntaxError:非法令牌非法” – epascarello
这不是有效的JSON。这将是一个有效的JavaScript文字,但它不是JSON。 – deceze