Json 无法从nginx lua获取正确的表键?
当前正在发布此数据:Json 无法从nginx lua获取正确的表键?,json,nginx,post,lua,Json,Nginx,Post,Lua,当前正在发布此数据: {“test”:“hello”,“test2”:“world”} 标题是: “内容类型:应用程序/json” 目前正在尝试循环使用json,但在我的代码中,出于某种原因,它已经将发布的数据视为表。所以,即使在我尝试将json编码和解码到表中时,它也是无用的 无论如何,当我循环通过它时,我只得到一个循环,其中关键点如下: "test":"hello" 我想知道这里出了什么问题。我尝试循环遍历每个键并将值设置为redis 它们应该是这样的: "test":"hello" 测
{“test”:“hello”,“test2”:“world”}
标题是:
“内容类型:应用程序/json”
目前正在尝试循环使用json,但在我的代码中,出于某种原因,它已经将发布的数据视为表
。所以,即使在我尝试将json编码和解码到表中时,它也是无用的
无论如何,当我循环通过它时,我只得到一个循环,其中关键点如下:
"test":"hello"
我想知道这里出了什么问题。我尝试循环遍历每个键并将值设置为redis
它们应该是这样的:
"test":"hello"
测试
这是我的内容\u by_lua
部分:
content_by_lua '
local cjson = require "cjson"
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000) -- 1 sec
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
ngx.header.content_type = "application/json; charset=utf-8"
ngx.req.read_body()
local data, err = ngx.req.get_post_args()
if not data then
ngx.say("err: ",err)
return
end
ngx.status = ngx.HTTP_OK
for key, val in pairs(data) do
ok, err = red:set(key, val)
if not ok then
ngx.say("failed to set key: ", err)
return
end
end
';
我在这里做错了什么?如果代码希望迭代一个Lua表,那么它将不会处理您正在测试的JSON格式字符串:
{“test”:“hello”,“test2”:“world”}
作为Lua表,它应该是:
{“test”=“hello”,“test2”=“world”}
现在,我无法从您发布的代码中看出为什么没有正确解码JSON等等