1
我正在使用luasec,lua-socket执行对外部API的请求,并将数据(JSON字符串)转换为的lua表。我已经阅读了所述模块的文档,但不幸的是,没有一篇文章帮助我解决了我的问题。无法链接超过2个网站与当前帐户,对不起。JSON字符串无法转换为正确的Lua表
摘要:当通过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只是输出功能的表到stdout。
在此先感谢
我已经运行了您的代码(由于缺少零件,Luar版本5.2,luasec,luasocket,luarocks中的lua-cjson也添加了缺少的零件和最少的编辑),并且它按预期工作。我建议用适当的方式调试它:使用调试器(ZBS或LDT)。 – Green
在ZBS的调试器中测试过它,它按预期工作。问题实际上是在我的打印表函数中有我的头表硬编码。除了我之外没有什么不对谢谢您的帮助 –