(转载) 游戏策划的excel配置表转成json文件(二)

使用python工具将excel生成的json文件,可以用在很多语言里,比如objective-c, C++,lua,javascript等等,来为游戏关卡配置数据。

如果是选择的lua语言,这个json文件还不能直接使用。因为json文件里只是json格式的字符串,需要先转换成lua里的数据格式:table。

将json文件转成table,需要利用第三方类库CJSON,代码如下:

[cpp] view plaincopy
 
  1. function tableFromFile (fileName )  
  2.         local t   
  3.         local jsonString  
  4.         local path = CCFileUtils:sharedFileUtils():fullPathForFilename(fileName)  
  5.         local myfile = io.open(path,"r")  
  6.         if myfile == nil then  
  7.             return nil  
  8.         end  
  9.         jsonString = myfile:read("*a")  
  10.         t = self.cjson.decode(jsonString)  
  11.         serialise_value(t)  
  12.         --print(table.entities[1].entity.HP)  
  13.         myfile.close()  
  14.         return t  
  15.     end  

比如json文件是这样的:

[cpp] view plaincopy
 
  1. {  
  2.     "entities": [  
  3.         {  
  4.             "entity": {  
  5.                 "carryRound": -1.0,   
  6.                 "target": "",   
  7.                 "additionValue": "",   
  8.                 "updateCondition": "",   
  9.                 "reduction": "",   
  10.                 "name": "普通物攻",   
  11.                 "job": "",   
  12.                 "gank": -1.0,   
  13.                 "effect": "",   
  14.                 "type": 2.0,   
  15.                 "id": 0.0,   
  16.                 "round": "",   
  17.                 "desc": ""  
  18.             }  
  19.         },   
  20.           
  21.     ]  
  22. }  

转成table后就可以类似这样使用

[cpp] view plaincopy
 
  1. local data = tableFromFile("xxx.json")   
  2. print(data.entities[1].entity.name )  

原文地址:http://blog.csdn.net/zhuangyou123/article/details/10069391

原文地址:https://www.cnblogs.com/wodehao0808/p/4228856.html