【问题标题】:JSON string fails to convert to correct Lua tableJSON 字符串无法转换为正确的 Lua 表
【发布时间】:2017-11-05 00:51:54
【问题描述】:

我正在使用 luasec lua-socket 向外部 API 执行请求,并将数据(JSON 字符串)转换为带有 cjson 的 lua 表。我已经阅读了上述模块的文档,不幸的是,这些文档都没有帮助我解决我的问题。无法将超过 2 个网站与当前帐户关联,抱歉。

总结:我使用发布的请求函数获得响应和适当的字符串,当通过 cjson.decode 将所述字符串转换为 lua 表时,输出表不是所需的,它是我的响应标头的副本,它不是故意的。

以下代码是我的请求方式:

local function request (req_t)
  local res_t = {}

  resp = https.request {
    url = const.API_URL .. req_t.url,
    method = req_t.method,
    headers = req_t.headers,
    sink = ltn12.sink.table(res_t)
  }

  return table.concat(res_t), resp.headers, resp.code
end

使用以下调用

local res, headers = request({ ... })

我收到了作为字符串的正确响应,但我的目标是使用它进行数据操作,因此将所述响应(字符串)转换为 lua 表

local resJson = cjson.decode(res)

不会产生正确的输出。它确实生成了一个与我的响应标头完全相同的表。这是我的终端的以下输出以及代码

When out of function type is: string

Desired response in string:
{"total_photos":221926,"photo_downloads":"186029632.0"}

When out of function type is: string

Desired response in string:
{"total_photos":221926,"photo_downloads":"186029632.0"}


After decode, type is: table

server  Cowboy
strict-transport-security   max-age=31536000
access-control-allow-headers    *
x-ratelimit-limit   50
x-ratelimit-remaining   46
x-cache-hits    0, 0
accept-ranges   bytes
access-control-request-method   *
x-request-id    ee5a74fd-2b10-4f46-9c25-5cfc53aeac6c
access-control-expose-headers   Link,X-Total,X-Per-Page,X-RateLimit-Limit,X-RateLimit-Remaining
content-type    application/json
connection  close
content-length  55
fastly-debug-digest f62d52c08b1ef74db89a66a0069f0a35c49e52230567905240dacf08c9ea1813
vary    Origin
cache-control   no-cache, no-store, must-revalidate
x-timer S1496524765.369880,VS0,VE111
x-cache MISS, MISS
x-served-by cache-iad2123-IAD, cache-mad9429-MAD
via 1.1 vegur, 1.1 varnish, 1.1 varnish
date    Sat, 03 Jun 2017 21:19:25 GMT
age 0
access-control-allow-origin *
x-runtime   0.011667

Printing header

server  Cowboy
strict-transport-security   max-age=31536000
access-control-allow-headers    *
x-ratelimit-limit   50
x-ratelimit-remaining   46
x-cache-hits    0, 0
accept-ranges   bytes
access-control-request-method   *
x-request-id    ee5a74fd-2b10-4f46-9c25-5cfc53aeac6c
access-control-expose-headers   Link,X-Total,X-Per-Page,X-RateLimit-Limit,X-RateLimit-Remaining
content-type    application/json
connection  close
content-length  55
fastly-debug-digest f62d52c08b1ef74db89a66a0069f0a35c49e52230567905240dacf08c9ea1813
vary    Origin
cache-control   no-cache, no-store, must-revalidate
x-timer S1496524765.369880,VS0,VE111
x-cache MISS, MISS
x-served-by cache-iad2123-IAD, cache-mad9429-MAD
via 1.1 vegur, 1.1 varnish, 1.1 varnish
date    Sat, 03 Jun 2017 21:19:25 GMT
age 0
access-control-allow-origin *
x-runtime   0.011667

产生上述日志的函数

local res, headers = request({ ... })

print('When out of function type is: ' ..type(res) .. '\n')
print('Desired response in string:')
print(res .. '\n')
resJson = cjson.decode(res)
print('\nAfter decode, type is: ' .. type(resJson) .. '\n')
pTable(resJson)
print('\nPrinting header\n')
pTable(headers)

pTable 只是一个将表格输出到标准输出的函数。

提前致谢

【问题讨论】:

  • 我已经运行了您的代码(添加了缺失的部分和由于缺失部分而进行的最小编辑;Lua 版本 5.2、luasec、luasocket、lua-cjson 来自 luarocks)并且它按预期工作。我建议以适当的方式对其进行调试:使用调试器(ZBS 或 LDT)。
  • 在 ZBS 的调试器中对其进行了测试,它按预期工作。问题实际上出在我的打印表函数中,该函数对我的标题表进行了硬编码。除了我没有错。感谢您的帮助

标签: json debugging lua luasocket cjson


【解决方案1】:

贴出的函数和例程是正确的。问题出在我的打印表函数中,我以某种方式硬编码了我的标题。

【讨论】:

    猜你喜欢
    • 2014-09-14
    • 2014-11-03
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 2015-02-28
    相关资源
    最近更新 更多