简体   繁体   English

无法解析JSON对象,Stringify或获取javascript属性

[英]Can't parse JSON Object, Stringify or get javascript property

I'm using a firefox addon that provides me with this variable: 我正在使用为我提供此变量的firefox插件:

[{errorMessage:"TypeError: a is null", sourceName:"http://pagead2.googlesyndication.com/pagead/js/graphics.js", 
lineNumber:17, console:null}]

From firebug, I can see this variable and its called "e". 从Firebug中,我可以看到此变量及其名为“ e”的变量。

I can type in e, and the print it as it is above. 我可以输入e,然后按上面的样子打印。

If I type e.toString(); 如果我输入e.toString(); I get, 我明白了

[object Object] [对象对象]

If I type e.errorMessage, it is undefined. 如果键入e.errorMessage,则它是未定义的。

If I type JSON.parse(e), I get unexpected character error. 如果键入JSON.parse(e),则会出现意外的字符错误。

How can I get the information out of this object? 如何从该对象中获取信息? It seems that anything I do to it, it just returns either [object Object] or undefined. 看来我所做的任何事情,它只会返回[object Object]或未定义。

I've tried JSON.parse, JSON.stringify, iterating through it, and nothing provides me with the actual object information. 我已经尝试过JSON.parse,JSON.stringify,对其进行迭代,但是没有任何东西可以为我提供实际的对象信息。

那是一个包含对象的数组,请尝试以下操作:

e[0].errorMessage;

From the JSON Specification: [RFC 4627][1] 来自JSON规范:[RFC 4627] [1]

2.2. 2.2。 Objects 对象

An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). 对象结构表示为一对大括号,包围着零个或多个名称/值对(或成员)。 A name is a string. 名称是一个字符串。 A single colon comes after each name, separating the name from the value. 每个名称后都有一个冒号,将名称与值分开。 A single comma separates a value from a following name. 单个逗号将值与后面的名称分开。 The names within an object SHOULD be unique. 对象中的名称应唯一。

2.5. 2.5。 Strings 字符串

A string begins and ends with quotation marks. 字符串以引号开头和结尾。


In the official ECMAScript specification: 在官方ECMAScript规范中:

It defines an Object Literal , which a PropertyNameAndValue can be a StringLiteral or an IdentifierLiteral. 它定义了一个Object Literal对象 ,PropertyNameAndValue可以是StringLiteral或IdentifierLiteral。 And an IdentifierLiteral , does not have quotes. 并且IdentifierLiteral ,没有引号。

Unquoted keys names are legal and allowed in Javascript but they are not valid JSON. 未加引号的键名称是合法的,并且在Javascript中允许使用,但它们不是有效的JSON。


http://jsonlint.com http://jsonlint.com

[
    {
        errorMessage: "TypeError: a is null",
        sourceName: "http://pagead2.googlesyndication.com/pagead/js/graphics.js",
        lineNumber: 17,
        console: null
    }
]

Results 结果

Parse error on line 3:
...a is null",        sourceName: "http://
----------------------^
Expecting 'STRING'

This is not JSON. 这不是JSON。 It is a JavaScript array. 这是一个JavaScript数组。 It has nothing to do with JSON in any way. 它与JSON无关。

To access a JavaScript array, just use ordinary JavaScript code, not JSON.parse or anything like that. 要访问JavaScript数组,只需使用普通的JavaScript代码,而不是JSON.parse或类似的东西。

You could use JSON.stringify() to turn this array into JSON, but that certainly isn't what you want. 您可以使用JSON.stringify()将此数组转换为 JSON,但这当然不是您想要的。

The reason e.toString() prints [object Object] is simply that this is what the .toString() method returns for an object or array. e.toString()打印[object Object]很简单,这就是.toString()方法为对象或数组返回的内容。 .toString() does not always give a useful result. .toString()并不总是给出有用的结果。

Paste the following into Firebug or the Chrome console and see what it logs: 将以下内容粘贴到Firebug或Chrome控制台中,查看其记录内容:

var e = [
    {
        errorMessage: "TypeError: a is null",
        sourceName: "http://pagead2.googlesyndication.com/pagead/js/graphics.js",
        lineNumber: 17,
        console:null
    }
];

console.log( e.length );
console.log( e[0] );
console.log( e[0].errorMessage );
console.log( e[0].sourceName );
console.log( e[0].lineNumber );

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

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