我有以下 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\"": "\"\"" }]
这是我的 Controller
$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 来修复项目数据,如下所示:
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;
};
关于javascript - angularjs用 "\"解析json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31881685/