如何使用 JavaScript 加载和解析服务器中的静态 JSON 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16288388/
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
How to use a JavaScript to load and parse a static JSON file in the server
提问by user2333683
I'm just starting to use javascript and json.
我刚开始使用 javascript 和 json。
I need to read data (getInformation function) from a json file when processing an event in a javascript function. So I need it to be synchronic. I don't know if I am missing something in the code, or if I have to create an Request and handle the callback, or if I need to import additional javascript to use json. Because I don't know how to make it work. It doesn't work because at the end the array is empty. Any help is aprreciated.
在 javascript 函数中处理事件时,我需要从 json 文件中读取数据(getInformation 函数)。所以我需要它是同步的。我不知道我是否在代码中遗漏了一些东西,或者我是否必须创建一个请求并处理回调,或者我是否需要导入额外的 javascript 来使用 json。因为我不知道如何让它工作。它不起作用,因为最后数组是空的。任何帮助表示赞赏。
The json file:
json文件:
{"Users": [
{"Name": "Jane",
"Points": 67,
"age": 23},
{
"Name": "Sam",
"Points": 65,
"age": 21}
]}
Option 1 - Function called by another function which is processing an event:
选项 1 - 由另一个正在处理事件的函数调用的函数:
var getInformation = function()
{
var path = "./data/users.json";
var informationArray= [];
console.log("Loading ....");
$.getJSON(path, function(data)
{
$.each(data, function(key, val)
{
informationArray.push(key + '-' + val);
});
});
return informationArray;
}
Option 2 - Function called by another function which is processing an event:
选项 2 - 由另一个正在处理事件的函数调用的函数:
var getInformation = function() {
var path = "./data/users.json";
var informationArray= [];
$.ajax({
url: path,
async: false,
dataType: 'json',
success: function(response) {
$.each(response.items,
function(item) {
informationArray.push(item);
});
informationArray.push("success");
}
});
return informationArray; }
I have seen the following thread and tried what is there but doens't work for me. I would like to know where is the problem in my code or if require any special configuration.
我已经看到以下线程并尝试了那里的内容但对我不起作用。我想知道我的代码中的问题出在哪里,或者是否需要任何特殊配置。
Thread: Is there a version of $getJSON that doesn't use a call back?
回答by olleicua
When JavaScript is running in a browser it needs to make an AJAX request to the server to access a JSON file. It is possible to write the AJAX request by hand but that is complex and difficult to make work in all browsers. Instead most people use a library like jQuery. You will need to include jQuery in your web page with something like:
当 JavaScript 在浏览器中运行时,它需要向服务器发出 AJAX 请求以访问 JSON 文件。可以手动编写 AJAX 请求,但在所有浏览器中都非常复杂且难以实现。相反,大多数人使用像 jQuery 这样的库。您需要在您的网页中包含 jQuery,例如:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
Then in any script tag lower in the html page you should be able to do something like:
然后在 html 页面下方的任何脚本标记中,您应该能够执行以下操作:
$.ajax({
url: "data/users.json",
dataType: "json",
success: function(response) {
$.each(response.Users, function(item) {
informationArray.push(item);
});
informationArray.push("success");
}
});
回答by Justin
To load a JSON file (and not require a callback) you'd use:
要加载 JSON 文件(并且不需要回调),您可以使用:
var url = 'http://yoursite.com/data/users.json';
var j = [];
$.ajax({
type: 'GET',
url: url,
dataType: 'json',
success: function(data) { j = data;},
async: false
});
alert(j.Users[0].Name);

