2017-06-03 128 views
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。

在此先感謝

+0

我已經運行了您的代碼(由於缺少零件,Luar版本5.2,luasec,luasocket,luarocks中的lua-cjson也添加了缺少的零件和最少的編輯),並且它按預期工作。我建議用適當的方式調試它:使用調試器(ZBS或LDT)。 – Green

+0

在ZBS的調試器中測試過它,它按預期工作。問題實際上是在我的打印表函數中有我的頭表硬編碼。除了我之外沒有什麼不對謝謝您的幫助 –

回答

0

已發佈的函數和例程是正確的。該問題位於我的打印表函數中,我以某種方式硬編碼了我的頭文件。