lua中使用json

Lua CJSON 是 Lua 语言提供高性能的 JSON 解析器和编码器,其性能比纯 Lua 库要高 10 到 20 倍。Lua CJSON 完全支持 UTF-8 ,无需依赖其他非 Lua/LuaJIT 的相关包

文档链接:
https://www.kyne.com.au/~mark/software/lua-cjson-manual.html#_installation
http://www.cnblogs.com/linxiong945/p/4107839.html


http://tooold.is-programmer.com/posts/38310.html

Lua-cjson的safe模块

在使用这个库函数的时候,最好使用2.1.0版(或者更新的)。
因为在2.1.0版新增了safe模块,在进行调用时,如果出现解析异常,不会抛出异常,而是返回Nil,这样可以避免程序意外退出。
如果直接采用cjson,如下:
local inputStr = '{"name":"void","br}'
...
local iJson = cjson.decode(inputStr)
...


程序将在cjson.decode()这一行意外退出,因为这里inputStr不是JSON格式数据,出现解析异常。
如果采用cjson.safe模块,如下:
local inputStr = '{"name":"void","br}'
...
local iJson = cjsonSafe.decode(inputStr)
...


这里虽然也出现了解析异常,但是由于cjson.safe返回的是nil,可以通过判断iJson是否为nil知道解析是否出现了问题。

http://blog.csdn.net/tietao/article/details/45015643

下载安装lua-cjson,lua操作json代码,指定require搜索路径
http://blog.csdn.net/guowenyan001/article/details/50848148

linux下搭建lua脚本语言的编程环境
http://blog.csdn.net/u011866460/article/details/43531171

猜你喜欢

转载自rd-030.iteye.com/blog/2380670