【发布时间】:2014-06-23 06:01:44
【问题描述】:
如何将本地文件系统中的 JSON 文件加载到 javascript 对象中?
类似于 jQuery 的东西:
$.getJSON( "ajax/test.json", function( data ) {
// data contains javascript object
});
【问题讨论】:
标签: json cocos2d-js
如何将本地文件系统中的 JSON 文件加载到 javascript 对象中?
类似于 jQuery 的东西:
$.getJSON( "ajax/test.json", function( data ) {
// data contains javascript object
});
【问题讨论】:
标签: json cocos2d-js
作为answered in the official forums,您可以拨打cc.loader.loadJson:
cc.loader.loadJson("res/example.json", function(error, data){
cc.log(data); //data is the json object
});
您作为参数传递的函数将在文件完成加载时被回调。
【讨论】:
研究这个我发现了以下(不完整的)解决方案:
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;
}
});
【讨论】: