[英]How to get JSON data from JSON file
I need to do an applet. 我需要做一个小程序。 This applet it is a stand reminder.
这个小程序是一个备用提醒。 I use a JSON file.
我使用JSON文件。 I need to do a dropdown menu so I did this code in my JSON file :
我需要做一个下拉菜单,所以我在JSON文件中做了以下代码:
`"questions": [
{
"key": "reminder",
"label": "Choose the time",
"help": "You can find the stock ticker on the web",
"required": true,
"order": 1,
"controlType": "dropdown",
"options":[10, 15, 20, 30, 40, 50, 60]
}
],`
The options is a list in order to allow the user to choose when he want an alert. 这些选项是一个列表,以便允许用户在需要警报时进行选择。 But I need to take the options in like an entry in my JS file, in order after to count down the time after with a function.
但是我需要像在我的JS文件中的条目一样接受这些选项,以便在以后用一个函数倒计时。 Can you help me please to find how can I take
options
like an entry and to display it a JS file? 您能帮我找到如何输入条目等
options
并将其显示为JS文件吗?
You can use fetch
to get the JSON file. 您可以使用
fetch
获取JSON文件。
fetch("../yourFile.JSON").then(res => res.json()).then(data => {
//do something with your JSON
});
fetch('https://jsonplaceholder.typicode.com/todos/1') .then(res => res.json()).then(json => { console.log(json); });
Newer browsers support the responseType
property of the XMLHttpRequest Object and you can set it to 'json' and then get the JSON response with response property of the XMLHttpRequest. 较新的浏览器支持XMLHttpRequest对象的
responseType
属性,您可以将其设置为“ json”,然后获取带有XMLHttpRequest的response属性的JSON响应。
Note: responseType='json'
is not supported by IE11 注意:IE11不支持
responseType='json'
var req = new XMLHttpRequest;
req.responseType = 'json';
req.open('GET', "../yourFile.JSON", true);
req.onload = function() {
var json = req.response;
// do something with your JSON
};
req.send(null);
var req = new XMLHttpRequest; req.responseType = 'json'; req.open('GET', "https://jsonplaceholder.typicode.com/todos/1", true); req.onload = function() { var json = req.response; console.log(json); // do something with your JSON }; req.send(null);
To support older browsers, you can use XMLHttpRequest and JSON.parse
to convert the responseText to JSON. 为了支持较旧的浏览器,可以使用XMLHttpRequest和
JSON.parse
将responseText转换为JSON。
var req = new XMLHttpRequest;
req.overrideMimeType("application/json");
req.open('GET', "../yourFile.JSON", true);
req.onload = function() {
var json = JSON.parse(req.responseText);
//do something with your JSON
};
req.send(null);
var req = new XMLHttpRequest; req.overrideMimeType("application/json"); req.open('GET', "https://jsonplaceholder.typicode.com/todos/1", true); req.onload = function() { var json = JSON.parse(req.responseText); console.log(json); //do something with your JSON }; req.send(null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.