听说cocos2dx本身的json.lua 效率比起 cjson要低70倍。且信其有。就接入cjson来用。
其实3.10本身已经有集成cjson了。在frameworks\cocos2d-x\external\lua\cjson 里。只不过没加入项目里以及导到lua。
只需要在自己工程的libuacocos2d工程,将cjson加入工程。
然后lua_extensions.c 文件里,加入头文件以及lua接口
就可以在lua使用了。
使用测试如下:(转自网上,测试可用)
编码Json
local cjson = require "cjson"
local retTable = {}; --最终产生json的表
--顺序数值
local intDatas = {};
intDatas[1] = 100;
intDatas[2] = "100";
--数组
local aryDatas = {};
aryDatas[1] = {};
aryDatas[1]["键11"] = "值11";
aryDatas[1]["键12"] = "值12";
aryDatas[2] = {};
aryDatas[2]["键21"] = "值21";
aryDatas[2]["键22"] = "值22";
--对Table赋值
retTable["键1"] = "值1";
retTable[2] = 123;
retTable["int_datas"] = intDatas;
retTable["aryDatas"] = aryDatas;
--将表数据编码成json字符串
local jsonStr = cjson.encode(retTable);
print(jsonStr);
--结果是:{"int_datas":[100,"100"],"2":123,"键1":"值1","aryDatas":[{"键12":"值12","键11":"值11"},{"键21":"值21","键22":"值22"}]}