我有以下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)}}
我的代码无法正常工作。有什么建议么?
最佳答案
为什么不执行以下操作?
$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;
};