JQuery,需要将stringify jason string拆分成数组

时间:2016-09-03 18:06:16

标签: javascript json multidimensional-array

我有一个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"
}]




2 个答案:

答案 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);