Answer the question
In order to leave comments, you need to log in
How to access JSON file?
I have a JSON file in the project folder, I need to access it and, for example, display the 'name' field in the console, I can't figure out how to do it. JSON looks something like this:
{
"projects": [
{
"name":"Алеша"
]
}
Answer the question
In order to leave comments, you need to log in
Smoke XMLHttpRequest, example:
function readTextFile(file, callback) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4 && rawFile.status == "200") {
callback(rawFile.responseText);
}
}
rawFile.send(null);
}
//использование:
readTextFile("/Users/Documents/workspace/test.json", function(text){
var data = JSON.parse(text);
console.log(data);
});
var json = JSON.parse(ваш_json_объект);
console.log( json.projects[0].name )
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question