2

How can I load a JSON file from the local filesystem into a javascript object?

Something similar to jQuery:

$.getJSON( "ajax/test.json", function( data ) {
    // data contains javascript object
}); 
4

2 に答える 2

0

これを調査したところ、次の(完全ではない)解決策が見つかりました:

var loadJSON = function(url, cb) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState==3 && xhr.status==200) {
            cb(null,JSON.parse(xhr.responseText));
        }
    }
    xhr.open("GET", url, true);
    xhr.send(null);
};

// read json file with words
loadJSON("res/words/dewords.words.json", function(err, text) {
    if( !err ) {
        muprisLayer.words = text;           
    }
});
于 2014-06-23T07:24:50.473 に答える