[英]Parsing JSON file to use in JavaScript
I am trying to parse a JSON file so I can access its data using JavaScript. 我正在尝试解析JSON文件,以便可以使用JavaScript访问其数据。 Here is a sample of the JSON file:
这是JSON文件的示例:
"Veg": {
"French beans 300g": 81969,
"Bunched beetroot": 78272,
"Squash box": 45450,
"Bunched carrots 500g": 90435,
"Courgettes x3": 17824,
"Fennel x1": 14816,
"Shiitake mushrooms 150g": 5166,
"Spring onions 125g": 65034,
"Red peppers x2": 11671,
An here is the JavaScript I am using: 这是我正在使用的JavaScript:
<script>
var sourceData;
// Initial data load
fetch("data.json", {
method: "GET",
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json'
})
}).then(function (body) {
return body.json();
}).then(function (data) {
sourceData = data;
});
</script>
But I am getting this error: 但我收到此错误:
Failed to load resource: Preflight response is not successful
and 和
Fetch API cannot load <file path> Preflight response is not successful
Try using 尝试使用
parseVariable = JSON.parse(JsonVariable);
console.log(parseVariable);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.