我有一个JSON格式的字符串。我想将所有值拆分为数组。
[{
"sno": "1",
"code": "bp150mb",
"quantity": null,
"name": "mudguard",
"company": "bajaj",
"vehicle": "pulsar",
"brand": "1",
"image": "N/A",
"color": "Black",
"price": "0"
}, {
"sno": "7",
"code": "P",
"quantity": "5",
"name": "",
"company": null,
"vehicle": "Not available",
"brand": "1",
"image": "N/A",
"color": "-",
"price": "0"
}]

答案 0 :(得分:0)
var str = '[{"sno":"1","code":"bp150mb","quantity":null,"name":"mudguard","company":"bajaj","vehicle":"pulsar","brand":"1","image":"N/A","color":"Black","price":"0"},{"sno":"7","code":"P","quantity":"5","name":"","company":null,"vehicle":"Not available","brand":"1","image":"N/A","color":"-","price":"0"}]';
var array = $.parseJSON(str);
OR
var array = jQuery.parseJSON(str);
因为问了jQuery。
答案 1 :(得分:0)
以下是您的需求:
var jsonString = "[{\"sno\":\"1\",\"code\":\"bp150mb\",\"quantity\":null,\"name\":\"mudguard\",\"company\":\"bajaj\",\"vehicle\":\"pulsar\",\"brand\":\"1\",\"image\":\"N/A\",\"color\":\"Black\",\"price\":\"0\"},{\"sno\":\"7\",\"code\":\"P\",\"quantity\":\"5\",\"name\":\"\",\"company\":null,\"vehicle\":\"Not available\",\"brand\":\"1\",\"image\":\"N/A\",\"color\":\"-\",\"price\":\"0\"}]";
var parsedJsonArray = JSON.parse(jsonString);
alert(parsedJsonArray[0]['name']);
console.log(parsedJsonArray);