cocos2d-x lua 3.6 解析json
发布时间:2020-12-14 16:55:15 所属栏目:百科 来源:网络整理
导读:cocos2d-x lua 解析json 在游戏开发过程中经常使用json文件作为存储本地数据的文件,这里介绍下cocos2d-x lua 3.6中如何使用json文件。 思路: 将json文件当作文本导入到程序中 通过引擎提供的json库解析json数据文件为table 代码 local str = cc.FileUtils:
cocos2d-x lua 解析json在游戏开发过程中经常使用json文件作为存储本地数据的文件,这里介绍下cocos2d-x lua 3.6中如何使用json文件。 思路:
代码 local str = cc.FileUtils:getInstance():getStringFromFile(config_file_name)
local data = json.decode(str)
self.data_ = data
for k,val in ipairs(data) do
table.foreach(val,function(i,v) print (i,v) end)
end
读取的json文件 [
{"id":1,"name":"dici","json":"dici.ExportJson","plist":"dici0.plist","png":"dici0.png"},{"id":2,"name":"ForeAnimation","json":"ForeAnimation.ExportJson","plist":"ForeAnimation0.plist","png":"ForeAnimation0.png"}
]
附加一段自动读取cocos的骨骼动画文件为json数据文件的python脚本.这个脚本的功能是自动查找当前文件夹,以及其子目录中的骨骼动画文件,并按照指定的格式在当前目录导出json文件。 #coding=utf-8
import os,sys,re
import json
file_path = []
def getNameList(dir,wildcard,recursion):
exts = wildcard.split(" ")
files = os.listdir(dir)
for name in files:
fullname = os.path.join(dir,name)
if os.path.isdir(fullname) & recursion:
getNameList(fullname,recursion)
else:
for ext in exts:
if(name.endswith(ext)):
file_path.append(fullname)
#print(fullname)
break
class AnimationData:
id = -1
name = ""
image_name = ""
plist_name = ""
json_name = ""
def __init__(self,id,name,image_name,plist_name,json_name):
self.id = id
self.image_name = image_name
self.plist_name = plist_name
self.json_name = json_name
self.name = name
def getDictionary(self):
ret = {}
ret["id"] = self.id
ret["name"] = self.name
ret["image"] = self.image_name
ret["plist"] = self.plist_name
ret["json"] = self.json_name
return ret
def run():
getNameList(sys.path[0],".ExportJson",1)
file_name_list = file_path
resList = []
id = 1
for file_name in file_name_list:
a = file_name.find(".ExportJson")
if a<>-1:
json_name = file_name.split("")[-1]
#print json_name
name = json_name.split(".")[0]
#print name
image_name = ("%s0.png")%(name)
plist_name = ("%s0.plist")%(name)
res = AnimationData(id,json_name)
resList.append(res)
id += 1
print id
l_out = []
for ani in resList:
l_out.append(ani.getDictionary())
print l_out
encode_json = json.dumps(l_out)
print encode_json
abs_path = os.path.abspath(sys.argv[0])
abs_path_list = abs_path.split("")
length = len(abs_path_list)
out_path = ""
for index,value in enumerate(abs_path_list):
if index >= length - 1:
break
if index == 0:
out_path = ("%s"%(value))
else:
out_path = ("%s%s"%(out_path,value))
out_path = ("%s%s"%(out_path,"AnimationConfig.json"))
print out_path
f = open(out_path,"w")
f.write(encode_json)
f.close()
if __name__ == "__main__":
run()
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |