简体   繁体   English

JSON.stringify无法正确转换array.object

[英]JSON.stringify not converting array.object correctly

I'm having a problem with JSON.stringify 我在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. 我试图从页面中拉出所有的meta标签,并将它们传递给firefox worker文件,以通过它们工作并返回一个对象。

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 然后我可以通过使用遍历metas对象

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. 发生的事情是我正常拉出meta标签,然后将JSON.stringify应用于可以推送给工作人员的对象。

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 在所有这些介绍性内容之后,这就是我的问题的根源:例如,以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 返回一个数组3个元素

[meta, meta, meta]

If I stringify it using: 如果我使用以下方式对其进行字符串化:

var jsonMetas = JSON.stringify(metas);

I would expect to jsonMetas to hold something like: 我希望jsonMetas可以保存以下内容:

{"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: 但是,当我查看jsonMetas对象时,看到此返回:

{"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? 我不太擅长JavaScript,所以请您解释一下(很简单的话:))。

Why does the stringify call return the unusually structured object? 为什么stringify调用返回异常结构化的对象? 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"
    }
]

The reason is that document.getElementsByTagName doesn't return JSON, it returns XML. 原因是document.getElementsByTagName不返回JSON,而是返回XML。 So, you'd need to use something like this to get your desired output: 因此,您需要使用类似以下的方法来获得所需的输出:

var jsonMetas = [];
for (i=0 ; i<metas.length ; i++) {
    var thisMeta = {};
    for (j=0 ; j<metas[i].attributes.length ; j++) {
        thisMeta[metas[i].attributes[j].name] = metas[i].attributes[j].value;
    }
    jsonMetas.push(thisMeta);
}

Output of JSON.stringify(jsonMetas) for this page: 此页面的JSON.stringify(jsonMetas)输出:

"[{"name":"relativepagescore","content":"0"},{"name":"title","content":"javascript - JSON.stringify not converting array.object correctly - Stack Overflow"}]" “ [{” name“:” relativepagescore“,” content“:” 0“},{” name“:” title“,” content“:” javascript-JSON.stringify无法正确转换array.object-堆栈溢出“} ]”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM