代码之家  ›  专栏  ›  技术社区  ›  Joshua Rajandiran

无法解码nginx lua中的json?

  •  0
  • Joshua Rajandiran  · 技术社区  · 6 年前

    当前通过发布此数据 curl :

    {"test2":"hello","test3":"world"}

    我的帖子是这样的:

    curl --header "Content-Type: application/json"   --request POST   --data '{"test2":"hello","test3":"world"}'   http://localhost/test
    

    以下是我请求的一部分:

        ngx.req.read_body()
        local data = ngx.req.get_body_data()
                     if not data then
                       ngx.say("err: ",err)
                       return
                     end
    
                     ngx.status = ngx.HTTP_OK
    
                     local eJson = cjson.encode(data)
    
                     local dJson = cjson.decode(eJson) -- decode json to lua table
                     ngx.say("Encoded: "..eJson);
                     ngx.say("Decoded: "..dJson);
    

    编码和解码的JSON输出:

    Encoded: "{\"test2\":\"hello\",\"test3\":\"world\"}" Decoded: {"test2":"hello","test3":"world"}

    我认为应该是这样的:

    {"test" = "hello", "test2" = "world"}
    

    我怎么会得到不同的输出?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Joshua Rajandiran    6 年前

    local dJson = cjson.decode(eJson) 
    -- first decode (made it from stringified JSON into a JSON)
    
    local d2Json = cjson.decode(dJson) 
    -- second decode (made it from JSON into a table)