[英]Read Json file into a variable
我想將JSON文件讀入變量。 我的JSON文件將命名為“ questions.json”,並將具有以下類型的內容:
{
question: "Question 1?",
answers: {
a: "A",
b: "B",
c: "The Correct One"
},
correctAnswer: "c"
},
{
question: "Question 2?",
answers: {
a: "A",
b: "B",
c: "The Correct One"
},
correctAnswer: "c"
}
我已經嘗試了許多方法來解決這個問題,例如:loadStrings,然后將字符串解析為JSON類型(告訴我loadStrings是未定義的),實際上我在網上找到的所有內容都是通過javascript讀取本地文件而nothign確實起作用。 。
那里出現了類似的問題:如果您正在使用jquery,則加載本地JSON文件 ,檢查那里的答案,也報告在這里:
$.getJSON("questions.json", function(json) {
console.log(json);
});
如果您僅使用javascript,請使用以下命令檢查來自xgqfrms的答案:
新的XMLHttpRequest();
您可以使用提取API:
fetch('http://example.com/movies.json')
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.