如何使用luvit读取URL的json

时间:2020-06-28 18:31:19

标签: json luvit

如何使用luvit读取JSON? 我尝试使用

http = require 'http'
json = http.parseUrl('https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCQmWt938Z8TkdPq6uasdlbA&type=video&eventType=live&key=[API_KEY]')
print(json)

我想要的输出是:

{
  "kind": "youtube#searchListResponse",
  "etag": "sFrPwpP_AW_VDxRSD3nOMOJhmmo",
  "regionCode": "BR",
  "pageInfo": {
    "totalResults": 0,
    "resultsPerPage": 5
  },
  "items": []
}

但是我得到一张桌子

1 个答案:

答案 0 :(得分:0)

嘿,我意识到了如何做到这一点

coro = require'coro-http'

local link = 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCgSAH94ZjV6-w46hPJ0-ffQ&type=video&eventType=live&key=[API_KEY]'

coroutine.wrap(function ()
   result, body = coro.request('GET', link)
   p(body)
   
 end)()

输入

'{\n  "kind": "youtube#searchListResponse",\n  "etag": "sFrPwpP_AW_VDxRSD3nOMOJhmmo",\n  "regionCode": "BR",\n  "pageInfo": {\n    "totalResults": 0,\n    "resultsPerPage": 5\n  },\n  "items": []\n}\n'