【问题标题】:cocos2d-js: How to load a JSON filecocos2d-js:如何加载 JSON 文件
【发布时间】:2014-06-23 06:01:44
【问题描述】:

如何将本地文件系统中的 JSON 文件加载到 javascript 对象中?

类似于 jQuery 的东西:

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

【问题讨论】:

    标签: json cocos2d-js


    【解决方案1】:

    作为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
    });
    

    您作为参数传递的函数将在文件完成加载时被回调。

    【讨论】:

      【解决方案2】:

      研究这个我发现了以下(不完整的)解决方案:

      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;           
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-23
        相关资源
        最近更新 更多