找回密码
 立即注册
首页 业界区 安全 OpenResty使用json模块解析json

OpenResty使用json模块解析json

晚能 2025-6-1 18:20:41
引入cjson模块

local json = require(“cjson”)
json.encode 将表格(table 包含哈希键值对 和 数组键值对)数据编码为 JSON 字符串
格式:
jsonString = json.encode(表格对象)
table转json字符串


  • table包含哈希键值对时,数组键值将被转换为字符串键值
    1. local json = require("cjson")
    2. local t = {1,3,name="张三",age="19",address={"地址1","地址2"},sex="女"}
    3. ngx.say(json.encode(t));
    4. ngx.say("<br/>");
    5. ----{"1":1,"2":3,"sex":"女","age":"19","address":["地址1","地址2"],"name":"张三"}
    复制代码
    1. local json = require("cjson")
    2. local str = json.encode({a=1,[5]=3})
    3. ngx.say(str); ----- {"a":1,"5":3}
    4. ngx.say("<br/>");
    复制代码
  • table所有键为数组型键值对时,会当作数组看待,空位将转化为null
    1. local json = require("cjson")
    2. local str = json.encode({[3]=1,[5]=2,[6]="3",[7]=4})
    3. ngx.say(str); ---- [null,null,1,null,2,"3",4]
    4. ngx.say("<br/>");
    5. local str = json.encode({[3]=2,[5]=3})
    6. ngx.say(str); ---- [null,null,2,null,3]
    7. ngx.say("<br/>");
    复制代码
json字符串转table

json.decode 将JSON 字符串解码为表格对象
格式:
table = json.decode(string)
  1. local json = require("cjson")
  2. local str  = [[ {"a":"v","b":2,"c":{"c1":1,"c2":2},"d":[10,11],"1":100} ]]
  3. local t    = json.decode(str)
  4. ngx.say(" --> ", type(t))
复制代码
  1. local json = require("cjson")
  2. local str = [[ {"a":1,"b":null} ]]
  3. local t    = json.decode(str)
  4. ngx.say(t.a, "<br/>")  
  5. ngx.say(t.b == nil, "<br/>")  
  6. ngx.say(t.b == json.null, "<br/>")
  7. ----> 1
  8. ----> false
  9. ----> true
复制代码
注意:null将会转换为json.null,实际就是null
异常处理


  • 异常复现
    1. local json = require("cjson")
    2. local str  = [[ {"key:"value"} ]]---少了一个双引号
    3. local t    = json.decode(str)
    4. ngx.say(" --> ", type(t))
    复制代码
    执行请求,看看效果,执行报了–500 Internal Server Error
    我们查一下nginx的error日志:是因为decode方法报错了导致。
    实际情况我们希望的结果不是报错,而是返回一个友好的结果,如返回个nil
  • 使用pcall命令
    如果需要在 Lua 中处理错误,必须使用函数 pcall(protected call)来包装需要执行的代码。
    pcall 接收一个函数和要传递给后者的参数,并执行,执行结果:有错误、无错误;
    返回值 true 或者或 false, errorinfo。
    pcall 以一种"保护模式"来调用第一个参数(函数),因此 pcall 可以捕获函数执行中的任何错误。
    第一个方案:重新包装一个 json decode编码
    1. local json = require("cjson")
    2. local function _json_decode(str)
    3.   return json.decode(str)
    4. end
    5. function json_decode( str )
    6.     local ok, t = pcall(_json_decode, str)
    7.     if not ok then
    8.       return nil
    9.     end
    10.     return t
    11. end
    12. local str  = [[ {"key:"value"} ]]---少了一个双引号
    13. local t    = json_decode(str)
    14. ngx.say(t)
    复制代码
    执行效果,没有系统错误,返回了nil
  • cjson.safe 模块

    引入cjson.safe 模块接口,该接口兼容 cjson 模块,并且在解析错误时不抛出异常,而是返回 nil。
    1. local json = require("cjson.safe")
    2. local str  = [[ {"key:"value"} ]]
    3. local t    = json.decode(str)
    4. if t then
    5.     ngx.say(" --> ", type(t))
    6. else
    7.         ngx.say("t is nil") -- 执行
    8. end
    复制代码
空table返回object还是array

测试一下,编码空table {}
  1. ocal json = require("cjson")
  2. ngx.say("value --> ", json.encode({}))
复制代码
输出 value --> {}
{}是个object;对于java的开发人员来说就不对了,空数组table,应该是[]
这个是因为对于 Lua 本身,是把数组和哈希键值对融合到一起了,所以他是无法区分空数组和空字典的。
要达到目标把 encode_empty_table_as_object 设置为 false
  1. local json = require("cjson")
  2. json.encode_empty_table_as_object(false)
  3. ngx.say("value --> ", json.encode({}))
复制代码
输出 value --> []

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册