openresty开发系列25--openresty中使用json模块 - reblue520 - 博客园


本站和网页 https://www.cnblogs.com/reblue520/p/11434258.html 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

openresty开发系列25--openresty中使用json模块 - reblue520 - 博客园
首页
新闻
博问
专区
闪存
班级
我的博客
我的园子
账号设置
简洁模式 ...
退出登录
注册
登录
reblue520
专注个人成长
博客园
新随笔
订阅
管理
openresty开发系列25--openresty中使用json模块
openresty开发系列25--openresty中使用json模块web开发过程中,经常用的数据结构为json,openresty中封装了json模块,我们看如何使用一)如何引入cjson模块,需要使用requirelocal json = require("cjson")json.encode 将表格数据编码为 JSON 字符串格式:jsonString = json.encode(表格对象)用法示例:table 包含哈希键值对 和 数组键值对-------------------test.lua--------------1)table包含哈希键值对时,数组键值将被转换为字符串键值local json = require("cjson")local t = {1,3,name="张三",age="19",address={"地址1","地址2"},sex="女"}ngx.say(json.encode(t));ngx.say("
");----{"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("
");2)table所有键为数组型键值对时,会当作数组看待,空位将转化为nulllocal str = json.encode({[3]=1,[5]=2,[6]="3",[7]=4})ngx.say(str); ---- [null,null,1,null,2,"3",4]ngx.say("
");local str = json.encode({[3]=2,[5]=3})ngx.say(str); ---- [null,null,2,null,3]ngx.say("
");----------------------------------------json.decode 将JSON 字符串解码为表格对象格式:table = json.decode(string)用法示例:local str = [[ {"a":"v","b":2,"c":{"c1":1,"c2":2},"d":[10,11],"1":100} ]]local t = json.decode(str)ngx.say(" --> ", type(t))-------------local str = [[ {"a":1,"b":null} ]]local t = json.decode(str)ngx.say(t.a, "
") ngx.say(t.b == nil, "
") ngx.say(t.b == json.null, "
")----> 1----> false----> true注意:null将会转换为json.null二)异常处理local json = require("cjson")local str = [[ {"key:"value"} ]]---少了一个双引号local t = json.decode(str)ngx.say(" --> ", type(t))执行请求,看看效果,执行报了--500 Internal Server Error是因为decode方法报错了导致实际情况我们希望的结果不是报错,而是返回一个友好的结果,如返回个nil使用pcall命令如果需要在 Lua 中处理错误,必须使用函数 pcall(protected call)来包装需要执行的代码。 pcall 接收一个函数和要传递给后者的参数,并执行,执行结果:有错误、无错误;返回值 true 或者或 false, errorinfo。pcall 以一种"保护模式"来调用第一个参数(函数),因此 pcall 可以捕获函数执行中的任何错误。第一个方案:重新包装一个 json decode编码local json = require("cjson")local function _json_decode(str) return json.decode(str)endfunction json_decode( str ) local ok, t = pcall(_json_decode, str) if not ok then return nil end return tendlocal str = [[ {"key:"value"} ]]---少了一个双引号local t = json_decode(str)ngx.say(t)执行效果,没有系统错误,返回了nil第二个方案:引入cjson.safe 模块接口,该接口兼容 cjson 模块,并且在解析错误时不抛出异常,而是返回 nil。local json = require("cjson.safe")local str = [[ {"key:"value"} ]]local t = json.decode(str)if t then ngx.say(" --> ", type(t))else ngx.say("t is nil")end三)空table返回object还是array测试一下,编码空table {}local json = require("cjson")ngx.say("value --> ", json.encode({}))输出 value --> {}{}是个object;对于java的开发人员来说就不对了,空数组table,应该是[]这个是因为对于 Lua 本身,是把数组和哈希键值对融合到一起了,所以他是无法区分空数组和空字典的。要达到目标把 encode_empty_table_as_object 设置为 falselocal json = require("cjson")json.encode_empty_table_as_object(false)ngx.say("value --> ", json.encode({}))输出 value --> []
posted @
2019-08-30 11:54
reblue520
阅读(3395)
评论(0)
编辑
收藏
举报
刷新评论刷新页面返回顶部
Copyright 2022 reblue520
Powered by .NET 7.0 on Kubernetes