-
Notifications
You must be signed in to change notification settings - Fork 376
/
Copy pathcjson-str-obj.lua
81 lines (69 loc) · 2.12 KB
/
cjson-str-obj.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
local cjson = require("cjson")
--lua对象到字符串
local obj = {
id = 1,
name = "zhangsan",
age = nil,
is_male = false,
hobby = {"film", "music", "read"}
}
local str = cjson.encode(obj)
ngx.say(str, "<br/>") -- 打印输出:{"hobby":["film","music","read"],"is_male":false,"name":"zhangsan","id":1}<br/>
--字符串到lua对象
str = '{
"hobby":["film","music","read"],"is_male":false,"name":"zhangsan","id":1,"age":null}'
local obj = cjson.decode(str) -- 字符串转换成一个对象
ngx.say(obj.age, "<br/>") -- 对象访问形式:obs.age
ngx.say(obj.age == nil, "<br/>") -- 数组访问形式:hobby[1]
ngx.say(obj.age == cjson.null, "<br/>")
ngx.say(obj.hobby[1], "<br/>")
--obj.obj
str_obj = '{"hobby":{"name":"tinywan","age":24,"reader":"AMAI"},"is_male":false}'
local obj_obj = cjson.decode(str_obj)
ngx.say(obj_obj.is_male, "<br/>")
ngx.say(obj_obj.hobby.name, "<br/>")
ngx.say(obj_obj.hobby.age, "<br/>")
ngx.say(obj_obj.hobby.reader, "<br/>")
--循环引用
obj = {
id = 1
}
obj.obj = obj
-- Cannot serialise, excessive nesting
--ngx.say(cjson.encode(obj), "<br/>")
local cjson_safe = require("cjson.safe")
--nil
ngx.say(cjson_safe.encode(obj), "<br/>")
--[[
打印结果:
root@tinywan:/opt/openresty/nginx/conf/Lua# curl http://127.0.0.1/dcjson
{"hobby":["film","music","read"],"is_male":false,"name":"zhangsan","id":1}<br/>
nil
null<br/>
false<br/>
true<br/>
film<br/>
false<br/>
tinywan<br/>
24<br/>
AMAI<br/>
nil<br/>
经验分享:
[1] lua对象到字符串 ,通过cjson.encode(obj)把所有的对象转换成了json格式,对象中的对象,被转换成了数组(数组作为 JSON 对象)
[2] JSON 对象中的数组
{
"name":"网站",
"num":3,
"sites":[ "Google", "Runoob", "Taobao" ]
}
[3] 嵌套 JSON 对象
myObj = {
"name":"runoob",
"alexa":10000,
"sites": {
"site1":"www.runoob.com",
"site2":"m.runoob.com",
"site3":"c.runoob.com"
}
}
--]]