angularjs用" \"解析json

时间:2015-08-07 15:40:28

标签: javascript json angularjs parsing

我有以下json(存在于$ scope.projectData中)。

[{
   "\"Space\"": "\"L1 (1 floor)\"",
   "\"Subject\"": "\"Corridor Distance\"",
   "\"Label\"": "\"Corridor Distance\"",
   "\"Color\"": "\"#33CCCC\"",
   "\"Length\"": "\"193.55\"",
   "\"Count\"": "\"0\"",
   "\"Multiplier\"": "\"1\"",
   "\"TotalFt\"": "\"193.55\"",
   "\"TotalCounts\"": "\"\"",
   "\"Notes\"": "\"\""},
{
   "\"Space\"": "\"L1 (1 floor)\"",
   "\"Subject\"": "\"Corridor Distance\"",
   "\"Label\"": "\"Corridor Distance\"",
   "\"Color\"": "\"#33CCCC\"",
   "\"Length\"": "\"210.36\"",
   "\"Count\"": "\"0\"",
   "\"Multiplier\"": "\"1\"",
   "\"TotalFt\"": "\"210.36\"",
   "\"TotalCounts\"": "\"\"",
   "\"Notes\"": "\"\"" }]

这是我的控制器

$scope.csvParse = function(item) {

    var array = JSON.parse(item);
     console.log(item);

  };

和我的HTML

{{ csvParse(projectData) }} 

我的代码无效。有什么建议吗?

1 个答案:

答案 0 :(得分:2)

为什么不执行以下操作?

 $scope.projectData = [{
   "Space": "L1 (1 floor)",
   "Subject": "Corridor Distance",
   "Label": "Corridor Distance",
   "Color": "#33CCCC",
   "Length": "193.55",
   "Count": "0",
   "Multiplier": "1",
   "TotalFt": "193.55",
   "TotalCounts": "",
   "Notes": ""},
{
   "Space": "L1 (1 floor)",
   "Subject": "Corridor Distance",
   "Label": "Corridor Distance",
   "Color": "#33CCCC",
   "Length": "210.36",
   "Count": "0",
   "Multiplier": "1",
   "TotalFt": "210.36",
   "TotalCounts": "",
   "Notes": "" }];

{{ projectData }} 

编辑:或者,您可以通过更改csvParse来修复projectData,如下所示:

function clean(str) {
  return str.replace(/\"/g, "")
}

$scope.csvParse = function(objs) {
  var newObjs = [];

  objs.forEach(function(obj) {
    var newObj = {};

    for(var key in obj) {
      val = obj[key];

      newObj[clean(key)] = clean(val);
    }

    newObjs.push(newObj);
  });

  return newObjs;
};