Javascript 中的 JSON 字符串解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8044965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:05:45  来源:igfitidea点击:

JSON string parsing in Javascript

javascriptjsonparsingservletsxmlhttprequest

提问by typedefcoder2

I have some JSON which has been passed from a servlet and is stored in xmlhttp.responseText. I want to decompose this JSON so that i can have values of data, size, style, name, etc. Also I wish to have the widget value to be in separate variable.

我有一些从 servlet 传递过来的 JSON 并存储在 xmlhttp.responseText 中。我想分解这个 JSON,以便我可以拥有数据、大小、样式、名称等值。我还希望小部件值位于单独的变量中。

Here is the JSON:

这是JSON:

{
  "widget vlaue=2": {
    "debug": "on",
    "window": {
      "title": "Sample Konfabulator Widget",
      "name": "main_window",
      "width": 500,
      "height": 500
    },
  },
  "image": { 
    "src": "Images/Sun.png",
    "name": "sun1",
    "hOffset": 250,
    "vOffset": 250,
    "alignment": "center"
  },
  "text": {
    "data": "Click Here",
    "size": 36,
    "style": "bold",
    "name": "text1",
    "hOffset": 250,
    "vOffset": 100,
    "alignment": "center",
    "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
  }
}    

I have tried this:

我试过这个:

obj = JSON.parse(xmlhttp.responseText); 

but this failed. I could not find anything related to it online. Can anyone please help me with this?

但这失败了。我在网上找不到与之相关的任何内容。任何人都可以帮我解决这个问题吗?

回答by Gapton

From json.org:

来自json.org

To convert a JSON text into an object, you can use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure. The text must be wrapped in parens to avoid tripping on an ambiguity in JavaScript's syntax.

var myObject = eval('(' + myJSONtext + ')');

要将 JSON 文本转换为对象,您可以使用 eval() 函数。eval() 调用 JavaScript 编译器。由于 JSON 是 JavaScript 的适当子集,编译器将正确解析文本并生成对象结构。文本必须用括号括起来,以避免在 JavaScript 语法中出现歧义。

var myObject = eval('(' + myJSONtext + ')');

However JSON.parse is still recommended:

但是 JSON.parse 仍然推荐:

The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted and competent. It is much safer to use a JSON parser. In web applications over XMLHttpRequest, communication is permitted only to the same origin that provide that page, so it is trusted. But it might not be competent. If the server is not rigorous in its JSON encoding, or if it does not scrupulously validate all of its inputs, then it could deliver invalid JSON text that could be carrying dangerous script. The eval function would execute the script, unleashing its malice.

To defend against this, a JSON parser should be used. A JSON parser will recognize only JSON text, rejecting all scripts. In browsers that provide native JSON support, JSON parsers are also much faster than eval. It is expected that native JSON support will be included in the next ECMAScript standard.

var myObject = JSON.parse(myJSONtext, reviver);

eval 函数非常快。但是,它可以编译和执行任何 JavaScript 程序,因此可能存在安全问题。当来源可信且有能力时,表明使用 eval。使用 JSON 解析器要安全得多。在通过 XMLHttpRequest 的 Web 应用程序中,只允许与提供该页面的同一来源进行通信,因此它是可信的。但它可能无法胜任。如果服务器的 JSON 编码不严格,或者它没有严格验证所有输入,那么它可能会传递可能带有危险脚本的无效 JSON 文本。eval 函数将执行脚本,释放其恶意。

为了防止这种情况,应该使用 JSON 解析器。JSON 解析器将仅识别 JSON 文本,拒绝所有脚本。在提供原生 JSON 支持的浏览器中,JSON 解析器也比 eval 快得多。预计本机 JSON 支持将包含在下一个 ECMAScript 标准中。

var myObject = JSON.parse(myJSONtext, reviver);

Perhaps there is something wrong with your JSON, visit jsonlint.comfor a free web-base JSON validator.

也许您的 JSON 有问题,请访问jsonlint.com获取免费的基于 Web 的 JSON 验证器。

回答by Strelok

Your JSON, that you provided is INVALID. Line 9 contains an extra ,that shouldn't be there.

您提供的 JSON 是INVALID。第 9 行包含一个,不应该出现的额外内容。

Here is a JSFiddle that works without the ,.

这是一个无需,.

http://jsfiddle.net/ApDsP/

http://jsfiddle.net/ApDsP/