如何从 JavaScript 读取脚本标签中的 JSON?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7581133/
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-08-24 02:48:37  来源:igfitidea点击:

How can I read a JSON in the script-tag from JavaScript?

javascriptjsonscript-tag

提问by Jonas

I have a dynamically generated page where I want to use a static JavaScript and pass it a JSON string as a parameter. I have seen this approach used by Google (see Google's +1 Button: How do they do it?).

我有一个动态生成的页面,我想在其中使用静态 JavaScript 并将 JSON 字符串作为参数传递给它。我已经看到 Google 使用的这种方法(请参阅Google 的 +1 按钮:他们是如何做到的?)。

But how should I read the JSON string from the JavaScript?

但是我应该如何从 JavaScript 中读取 JSON 字符串?

<html>
  <head>
    <script src="jquery-1.6.2.min.js"></script>
    <script src="myscript.js">{"org": 10, "items":["one","two"]}</script>
  </head>
  <body>
    Hello
  </body>
</html>

In this JavaScript I would like to use the JSON argument {"org": 10, "items":["one","two"]}from the HTML document. I don't know if it's best to do it with jQuery or without.

在这个 JavaScript 中,我想使用{"org": 10, "items":["one","two"]}HTML 文档中的 JSON 参数。我不知道最好用 jQuery 还是不用 jQuery。

$(function() {
    // read JSON

    alert("the json is:")
})

回答by c-smile

I would change the script declaration to this:

我会将脚本声明更改为:

<script id="data" type="application/json">{"org": 10, "items":["one","two"]}</script>

Note type and id fields. After that

注意类型和 ID 字段。在那之后

var data = JSON.parse(document.getElementById('data').innerHTML);

will work just fine in all browsers.

在所有浏览器中都能正常工作。

The type="application/json"is needed to prevent browser from parsing it while loading.

type="application/json"需要,以防止浏览器解析它,而加载。

回答by Jonas

I ended up with this JavaScript code to be independent of jQuery.

我最终将这段 JavaScript 代码独立于 jQuery。

var json = document.getElementsByTagName('script');
var myObject = JSON.parse(json[json.length-1].textContent);

回答by Zekrom_Vale

To read JSON in <script id="myJSON">use

读取正在<script id="myJSON">使用的JSON

var manifest= document.getElementById('myJSON').innerHTML; //sets manifest to the text in #myJSON
manifest= JSON.parse(manifest) //Converts text into JSON

You can also use methods to point to the script like document.scripts[0]

您还可以使用方法来指向脚本,如 document.scripts[0]

    //var manifest= JSON.parse(document.getElementById('myJSON').innerHTML); /*Shortend of 2&3*/
var manifest= document.getElementById('myJSON').innerHTML; //Gets text in #myJSON
manifest= JSON.parse(manifest) //Converts it into JSON
document.getElementById('test').innerHTML= manifest.name+ '<br/>'+ manifest.otherOptions; //Displays it
console.log('manifest')
console.log(manifest);
<head>
<script type="application/json" id="myJSON">
  {"name":"Web Starter Kit", "otherOptions":"directly here"}
</script>
</head>
<body>
<p id="test"></p>
</body>

回答by Emil Ivanov

JSON.parse($('script[src="mysript.js"]').html());

or invent some other method to identify the script.

或者发明一些其他方法来识别脚本。

Maybe instead of .html()you might need .text(). Not sure. Try them both.

也许而不是.html()你可能需要.text(). 没有把握。两个都试试。