在游戏开发过程中经常使用Json文件作为存储本地数据的文件,这里介绍下cocos2d-x lua 3.6中如何使用Json文件。
思路:
将Json文件当作文本导入到程序中 通过引擎提供的Json库解析Json数据文件为table代码
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-8import os,sys,reimport Jsonfile_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) breakclass 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 retdef 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()总结
以上是内存溢出为你收集整理的cocos2d-x lua 3.6 解析json全部内容,希望文章能够帮你解决cocos2d-x lua 3.6 解析json所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)