JSON.stringify无法正确转换array.object [英] JSON.stringify not converting array.object correctly
问题描述
我在JSON.stringify上遇到问题
我正在尝试将所有meta标签从页面中拉出,并将它们传递给firefox工作程序文件,以通过它们工作并返回一个对象.
因此,以前没有工作人员运行我的代码时,只有在我转为使用工作程序文件时,我的代码才出现(由于无法进入的原因,我需要使用工作程序!)>
所以以前我会使用
来获取所有元标记.var metas = document.getElementsByTagName("meta");
然后我可以通过使用
遍历metas对象for (var index in metas){
var currentMeta = metas[index];
//(and so on, this code worked perfectly)
当我移至外部工作程序文件方案时,会发生问题. 发生了什么事,我照常拉出了meta标签,然后将JSON.stringify应用于可以推送给工作人员的对象.
在介绍完所有内容之后,这就是我的问题的根源: 例如,我在html内带有以下代码的页面上
<meta content="width=1024" name="viewport">
<meta charset="UTF-8">
<meta content="Mozilla Hacks – the Web developer blog" name="title">
如果运行以下代码,我将得到一个数组
var metas = document.getElementsByTagName("meta");
返回3个元素的数组
[meta, meta, meta]
如果我使用:
var jsonMetas = JSON.stringify(metas);
我希望jsonMetas可以保存以下内容:
{"0":{"content":"width=1024","name":"viewport"},"1":{"charset":"UTF-8"},"2":{"content":"Mozilla Hacks - the web developer blog","name":"title"} }
但是,当我查看jsonMetas对象时,我看到此返回:
{"0":{"constructor":{}},"1":{"constructor":{}},"2":{"constructor":{}}}
呵呵?????
我不太擅长JavaScript,所以请您解释一下(很简单):)发生了什么事?
为什么stringify调用返回异常结构化的对象? 我在做什么错了?
预先感谢您的回答.
您可以使用以下:
var metas = document.getElementsByTagName("meta");
var arr = [];
for (var i = 0; i < metas.length; i++) {
var obj = {};
for (var j = 0; j < metas[i].attributes.length; j++) {
var att = metas[i].attributes[j];
obj[att.name] = att.value;
}
arr.push(obj);
}
var jsonMetas = JSON.stringify(arr);
console.log(jsonMetas);
导致:
[
{
"http-equiv": "content-type",
"content": "text/html; charset=UTF-8"
},
{
"content": "width=1024",
"name": "viewport"
},
{
"charset": "UTF-8"
},
{
"content": "Mozilla Hacks – the Web developer blog",
"name": "title"
}
]
I'm having a problem with JSON.stringify
I'm trying to pull all the meta tags out of the page and pass them to a firefox worker file to work through them and to return back an object.
So my code previously worked when I didn't have a worker running the issue has only cropped up when I have moved to using worker file (for reasons I can't go into I need to use a worker!)
So previously I would get all the meta tags using
var metas = document.getElementsByTagName("meta");
then I could loop through the metas object by using
for (var index in metas){
var currentMeta = metas[index];
//(and so on, this code worked perfectly)
The problem occurs when I move to the external worker file scenario. What happens is I pull the meta tags out as normal, and then I use JSON.stringify to something that I can push to the worker.
After all that intro blab, here is the root of my problem: Take for example that I land on a page with the following code within the html
<meta content="width=1024" name="viewport">
<meta charset="UTF-8">
<meta content="Mozilla Hacks – the Web developer blog" name="title">
If I run the following code I get an array
var metas = document.getElementsByTagName("meta");
returns an array 3 elements
[meta, meta, meta]
If I stringify it using:
var jsonMetas = JSON.stringify(metas);
I would expect to jsonMetas to hold something like:
{"0":{"content":"width=1024","name":"viewport"},"1":{"charset":"UTF-8"},"2":{"content":"Mozilla Hacks - the web developer blog","name":"title"} }
However when I look at the jsonMetas object I see this returned:
{"0":{"constructor":{}},"1":{"constructor":{}},"2":{"constructor":{}}}
Huh?????
I'm not that good at JavaScript, so could you please explaining (in very small words :) ) what is going on?
Why does the stringify call return the unusually structured object? What am I doing wrong?
Thanks in advance for your answers.
You could use the following:
var metas = document.getElementsByTagName("meta");
var arr = [];
for (var i = 0; i < metas.length; i++) {
var obj = {};
for (var j = 0; j < metas[i].attributes.length; j++) {
var att = metas[i].attributes[j];
obj[att.name] = att.value;
}
arr.push(obj);
}
var jsonMetas = JSON.stringify(arr);
console.log(jsonMetas);
results in:
[
{
"http-equiv": "content-type",
"content": "text/html; charset=UTF-8"
},
{
"content": "width=1024",
"name": "viewport"
},
{
"charset": "UTF-8"
},
{
"content": "Mozilla Hacks – the Web developer blog",
"name": "title"
}
]
这篇关于JSON.stringify无法正确转换array.object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!