JSON 数组未正确字符串化
JSON array does not Stringify properly
在我的 Nodejs 站点中,我创建了一个 JSON 数组并将其发送到 angularJs 客户端。然后它显示为 [object, object]。由于这个原因,我使用 JSON.stringify 现在它在 angularJs 客户端中以一种奇怪的方式显示。
在NodeJs端创建数组,
getAttachments = function () {
let attach = [];
workflowSession.files.forEach(file => {
if (file) {
const item = {
text: file.name,
link: workflowSession.metadata.portalBaseUrl + "api/files/" + workflowSession.workflowRef + "@"+ file.name
}
attach.push(item);
}
});
return attach;
};
当我将控制台日志放在 Nodejs 端时,它显示如下,
[ { text: 'sdee.png',
link: 'http://localhost:3000/api/files/kYnDsXjyFT@sdee.png' },
{ text: 'wes.png',
link: 'http://localhost:3000/api/files/kYnDsXjyFT@wes.png' }
]
但是在AngularJs客户端中显示为[object, object],因此我对上面的数组创建方法做了如下更改,
getAttachments = function () {
let attach = [];
workflowSession.files.forEach(file => {
if (file) {
const item = {
text: file.name,
link: workflowSession.metadata.portalBaseUrl + "api/files/" + workflowSession.workflowRef + "@"+ file.name
}
attach.push(item);
}
});
return JSON.stringify(attach);
};
然后 NodeJs 端显示 console.log 的数据如下,
[
{"text":"sdee.png","link":"http://localhost:3000/api/files/kYnDsXjyFT@sdee.png"}, {"text":"wes.png","link":"http://localhost:3000/api/files/kYnDsXjyFT@wes.png"}
]
但在AngularJs方面显示如下,
[
{"text":"wes.png","link":"http://localhost:3000/api/files/saJiCDZPet@wes.png"},{"text":"ssdd.png","link":"http://localhost:3000/api/files/saJiCDZPet@ssdd.png"}
]
我想按照下面的 HTML 片段所示迭代此数组,但由于格式问题我无法这样做。 "action.links" 是从 Nodejs 端创建的数组。
<md-menu-content>
<md-menu-item ng-repeat="item in action.links" ng-if="item.link">
<md-button href="{{item.link}}" target="_blank">
{{item.text}}
</md-button>
</md-menu-item>
</md-menu-content>
我做错了什么?
看起来 Angular 端正在 HTML 编码,您可以做的是 运行 通过 HTML 解码:
var htmlDecode = function(str) {
var temp = document.createElement('span');
temp.innerHTML = str;
return temp.textContent;
};
var foo = '[{"text":"wes.png","link":"http://localhost:3000/api/files/saJiCDZPet@wes.png"},{"text":"ssdd.png","link":"http://localhost:3000/api/files/saJiCDZPet@ssdd.png"}]';
console.log(
htmlDecode(foo)
);
理想情况下,您首先要避免对其进行 HTML 编码。如果您能够共享代码的实时版本,则可以更轻松地调试发生这种情况的原因。
您不需要在 node.js 端对 JS 对象进行字符串化。根据您提供的内容,您在登录控制台时看到的 [object, object]
语法可能没问题。当数组作为对象数组返回时,对数组的迭代是否不起作用?
如果您想在客户端使用 console.log
查看数据,请在此处对对象进行字符串化:
console.log(JSON.stringify(action.links));
您将无法遍历 JSON 数组,因为它是一个字符串,需要先解析回对象数组。
在我的 Nodejs 站点中,我创建了一个 JSON 数组并将其发送到 angularJs 客户端。然后它显示为 [object, object]。由于这个原因,我使用 JSON.stringify 现在它在 angularJs 客户端中以一种奇怪的方式显示。
在NodeJs端创建数组,
getAttachments = function () {
let attach = [];
workflowSession.files.forEach(file => {
if (file) {
const item = {
text: file.name,
link: workflowSession.metadata.portalBaseUrl + "api/files/" + workflowSession.workflowRef + "@"+ file.name
}
attach.push(item);
}
});
return attach;
};
当我将控制台日志放在 Nodejs 端时,它显示如下,
[ { text: 'sdee.png',
link: 'http://localhost:3000/api/files/kYnDsXjyFT@sdee.png' },
{ text: 'wes.png',
link: 'http://localhost:3000/api/files/kYnDsXjyFT@wes.png' }
]
但是在AngularJs客户端中显示为[object, object],因此我对上面的数组创建方法做了如下更改,
getAttachments = function () {
let attach = [];
workflowSession.files.forEach(file => {
if (file) {
const item = {
text: file.name,
link: workflowSession.metadata.portalBaseUrl + "api/files/" + workflowSession.workflowRef + "@"+ file.name
}
attach.push(item);
}
});
return JSON.stringify(attach);
};
然后 NodeJs 端显示 console.log 的数据如下,
[
{"text":"sdee.png","link":"http://localhost:3000/api/files/kYnDsXjyFT@sdee.png"}, {"text":"wes.png","link":"http://localhost:3000/api/files/kYnDsXjyFT@wes.png"}
]
但在AngularJs方面显示如下,
[
{"text":"wes.png","link":"http://localhost:3000/api/files/saJiCDZPet@wes.png"},{"text":"ssdd.png","link":"http://localhost:3000/api/files/saJiCDZPet@ssdd.png"}
]
我想按照下面的 HTML 片段所示迭代此数组,但由于格式问题我无法这样做。 "action.links" 是从 Nodejs 端创建的数组。
<md-menu-content>
<md-menu-item ng-repeat="item in action.links" ng-if="item.link">
<md-button href="{{item.link}}" target="_blank">
{{item.text}}
</md-button>
</md-menu-item>
</md-menu-content>
我做错了什么?
看起来 Angular 端正在 HTML 编码,您可以做的是 运行 通过 HTML 解码:
var htmlDecode = function(str) {
var temp = document.createElement('span');
temp.innerHTML = str;
return temp.textContent;
};
var foo = '[{"text":"wes.png","link":"http://localhost:3000/api/files/saJiCDZPet@wes.png"},{"text":"ssdd.png","link":"http://localhost:3000/api/files/saJiCDZPet@ssdd.png"}]';
console.log(
htmlDecode(foo)
);
理想情况下,您首先要避免对其进行 HTML 编码。如果您能够共享代码的实时版本,则可以更轻松地调试发生这种情况的原因。
您不需要在 node.js 端对 JS 对象进行字符串化。根据您提供的内容,您在登录控制台时看到的 [object, object]
语法可能没问题。当数组作为对象数组返回时,对数组的迭代是否不起作用?
如果您想在客户端使用 console.log
查看数据,请在此处对对象进行字符串化:
console.log(JSON.stringify(action.links));
您将无法遍历 JSON 数组,因为它是一个字符串,需要先解析回对象数组。