使用lua CJSON库如何将空table编码成数组

来源:互联网 发布:三国群英传2数据修改器 编辑:程序博客网 时间:2024/05/18 09:40

先看下面一段代码

 local cjson = require "cjson"  print(cjson.encode({dogs = {}})) -- 输出 {"dogs":{}} 

lua的CJSON库默认会将空table编码成{},如果dogs是一个数组呢?当dogs为空的时候,如何让它返回一个空数组?很简单,加多一句代码就可以了

 local cjson = require "cjson"  cjson.encode_empty_table_as_object(false)  print(cjson.encode({dogs = {}})) -- 输出 {"dogs":[]} 
0 0
原创粉丝点击