admin管理员组文章数量:1357148
I have a JSON data which I can get from this API. However when I copy the contents of this link into a local file and save it as JSON, and then import in in JS like:
import * as data from "./data.json" assert { type: "json" };
console.log(data)
I am getting a module in my console where the default parameter contains my JSON object.
But when I do:
import * as data from "./data.json" assert { type: "json" };
console.log(data.default)
Then only I can view my JSON object like normal, i.e. what would the output have been if I did:
async function populate() {
const requestURL = '.json';
const request = new Request(requestURL);
const response = await fetch(request);
const superHeroes = await response.json();
console.log(superHeroes)
}
populate();
I have a JSON data which I can get from this API. However when I copy the contents of this link into a local file and save it as JSON, and then import in in JS like:
import * as data from "./data.json" assert { type: "json" };
console.log(data)
I am getting a module in my console where the default parameter contains my JSON object.
But when I do:
import * as data from "./data.json" assert { type: "json" };
console.log(data.default)
Then only I can view my JSON object like normal, i.e. what would the output have been if I did:
async function populate() {
const requestURL = 'https://mdn.github.io/learning-area/javascript/oojs/json/superheroes.json';
const request = new Request(requestURL);
const response = await fetch(request);
const superHeroes = await response.json();
console.log(superHeroes)
}
populate();
Why does this happen? And is there any better method of importing my JSON file so that I can access it in my code?
Share Improve this question asked Feb 6, 2022 at 16:42 ritwick_ _Dritwick_ _D 1274 silver badges12 bronze badges 1-
Because if I do so, and try to console.log the object I get the error:
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "application/json". Strict MIME type checking is enforced for module scripts per HTML spec.
. A quick google search led me to useassert { type: "json" }
– ritwick_ _D Commented Feb 6, 2022 at 17:26
1 Answer
Reset to default 8This happens because the import * as
line imports an_entire_modules_contents where cause the absence of an explicit export default
the default property coincides with the content of your json file. If you want the data
variable contains your json file you can use instead:
import {default as data} from "./data.json";
console.log(data); //<-- it will print the content of your json file
Or the simple shorthand below:
import data from "./data.json";
本文标签: javascriptWhy is default required in importing JSON file in JS codeStack Overflow
版权声明:本文标题:javascript - Why is default required in importing JSON file in JS code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744073788a2586368.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论