使用python工具将excel生成的json文件,可以用在很多语言里,比如objective-c, C++,lua,javascript等等,来为游戏关卡配置数据。
如果是选择的lua语言,这个json文件还不能直接使用。因为json文件里只是json格式的字符串,需要先转换成lua里的数据格式:table。
将json文件转成table,需要利用第三方类库CJSON,代码如下:
function tableFromFile (fileName )
local t
local jsonString
local path = CCFileUtils:sharedFileUtils():fullPathForFilename(fileName)
local myfile = io.open(path,"r")
if myfile == nil then
return nil
end
jsonString = myfile:read("*a")
t = self.cjson.decode(jsonString)
serialise_value(t)
--print(table.entities[1].entity.HP)
myfile.close()
return t
end
比如json文件是这样的:
{
"entities": [
{
"entity": {
"carryRound": -1.0,
"target": "",
"additionValue": "",
"updateCondition": "",
"reduction": "",
"name": "普通物攻",
"job": "",
"gank": -1.0,
"effect": "",
"type": 2.0,
"id": 0.0,
"round": "",
"desc": ""
}
},
]
}
转成table后就可以类似这样使用
local data = tableFromFile("xxx.json")
print(data.entities[1].entity.name )