[Openresty json module]

. 1, describes json
web development process, the data structure is often used json, openresty json module encapsulates
how to use openresty

local json = require("cjson")

JSON.encode the table data encoded string JSON
format: jsonString = json.encode (table object)

2, the specific case
when the key-value pair contains a hash, the key array is converted to a string key 2.1 table

local json = require("cjson")
local t = {1,3,name="张三",age="19",address={"地址1","地址2"},sex="女"}
ngx.say(json.encode(t));
ngx.say("<br/>");
# 输出结果
{"1":1,"2":3,"sex":"女","age":"19","address":["地址1","地址2"],"name":"张三"}


local str = json.encode({a=1,[5]=3})
ngx.say(str); ----- {"a":1,"5":3}
ngx.say("<br/>");

2.2 table when all keys are array key-value pair will look as an array, the vacancy will be converted to null

local str = json.encode({[3]=1,[5]=2,[6]="3",[7]=4})
ngx.say(str); ---- [null,null,1,null,2,"3",4]
ngx.say("<br/>");

local str = json.encode({[3]=2,[5]=3})
ngx.say(str); ---- [null,null,2,null,3]
ngx.say("<br/>");
He published 192 original articles · won praise 18 · views 80000 +

Guess you like

Origin blog.csdn.net/luolinll1212/article/details/103970646