Skip to content

Commit 6790d24

Browse files
committed
json 和 lua table 转换
1 parent 794c085 commit 6790d24

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
+ [lua-resty-mysql 扩展](#Openresty_resty-mysql)
4646
+ [lua-resty-shell 扩展](http://www.cnblogs.com/tinywan/p/6809879.html)
4747
+ [lua-resty-template 扩展](https://github.com/Tinywan/lua_project_v0.01)
48+
+ [lua-resty-template 扩展](https://github.com/Tinywan/lua_project_v0.01)
4849
+ [openresty扫描代码全局变量](#Openresty_all-var)
4950
+ [ngx Lua APi 方法和常量](#Openresty_http_status_constants)
5051
+ ngx_lua 核心常量
@@ -368,6 +369,25 @@
368369
id = 2
369370
]]
370371
```
372+
+ json 和 lua table 转换
373+
+ [1] 将 json 转换成 lua table
374+
```lua
375+
local json_str = '{"is_male":"nan","name":"zhangsan","id":1}'
376+
local t = json.decode(json_str)
377+
ngx.say(format_table(t))
378+
```
379+
+ [2] 将 lua table 转换成 json 字符串
380+
```lua
381+
local t = [[{key="table key",value="table value"}]]
382+
local json_str = json.encode(t)
383+
ngx.say(json_str) -- "{key=\"table key\",value=\"table value\"}"
384+
```
385+
+ [3] 将lua table转换成 json 数组 (lua 两个大括号表示一个数组)
386+
```lua
387+
local t = {keys={"list1","list2","list3"},num=1}
388+
local str = json.encode(t)
389+
ngx.say(str) -- {"keys":["list1","list2","list3"],"num":1}
390+
```
371391
+ 编译执行与错误
372392
+ error 错误
373393
```lua

0 commit comments

Comments
 (0)