Cocos Creator加载读取Json文件

 1 cc.Class({
 2     extends: cc.Component,
 3 
 4     properties: {
 5 
 6     },
 7 
 8     onLoad () {
 9         this.test="";
10         // var self = this;
11         cc.loader.load(cc.url.raw('resources/B3U3XML.json'), (err,object) => {
12             // cc.loader.load(cc.url.raw('resources/B3U3XML.json'), function (err,object) {
13             // (err,object)为回调方法,得出结果,在此注意,应避免用function,因为这样声明函数作用域会过小,导致其他空间调用此赋值无效
14             if (err) {
15                 cc.log(err);
16             }
17             else{
18                 cc.log("我有数据啦");
19  
20                 let root = object.B3U3Amazing_Watch;
21                 let s = root.tSounds;
22                 let t = root.tAnims;
23                 let ta = root.tAddClicks;
24                 let ts = root.tShows;
25                 let tscrip = root.tScriptIndexs;
26                 let guid = root.Guideparas;
27                 
28                 cc.log("tSounds:" + s + ", tAnims: " + t + ", tAddClicks: " +  ta + ", tShows: " + ts + ", tScriptIndexs: " + tscrip + ", Guideparas: " + guid);
29                 cc.log("========================================");
30                 
31                 // self.test = s;   使用function时的方法
32                 // self.init();
33 
34                 this.test = root;   // 使用(err,object) =>时的方法
35                 this.init();
36 
37             }
38         });
39     },
40 
41     start () {
42     },
43 
44     init: function(){
45         // this.test获取整个Json数据
46         cc.log("获取数据:" + this.test.tAnims);
47     },
48 
49     // update (dt) {},
50 });

Json列表

 1 {
 2     "B3U3Amazing_Watch": 
 3     {
 4         "tSounds": "1,22031;6,22036;7,22034;9,22032;12,238;14,22033",
 5         "tAnims": "w,2,7,PlaySetAnim;sp,2,11,PlaySetAnimOnce;flyw,2,8,PlaySetAnimOnce;flagweb,2,6,PlaySetAnimOnce;flagsp,2,12,PlaySetAnimOnce;zouzi,2,1,PlaySetAnimOnce;sp2,2,14,PlaySetAnim;sp,2,14,Active,1",
 6         "tAddClicks": "sp2/w/web/zouzi",
 7         "tShows": "w",
 8         "tScriptIndexs":"22",
 9         "Guideparas":"450/30/4"
10     }
11 
12 
13 
14 }

猜你喜欢

转载自www.cnblogs.com/Hunter-541695/p/9593877.html