在 Lua 中拆分 table

Splitting a table in Lua

我有一个脚本,它为我提供了来自网站 API 的时间戳和价格的 table,我想将它们分别拆分为时间戳和价格。

我获取价格和时间戳的代码

local socket = require('socket.http')
local requests = require('requests')
local json = require('cjson')
local https = require('ssl.https')

local body, code, headers, status = https.request(URL)

if body then
print(body)
else
print(code)
end

输出:

{"success":true,"data":{"ee":[{"timestamp":1649192400,"price":55.0000},{"timestamp":1649196000,"price":60.0800},{"timestamp":1649199600,"price":60.0800},{"timestamp":1649203200,"price":99.7900},{"timestamp":1649206800,"price":99.7000},{"timestamp":1649210400,"price":104.9700},{"timestamp":1649214000,"price":118.4800},{"timestamp":1649217600,"price":165.6000},{"timestamp":1649221200,"price":177.8400},{"timestamp":1649224800,"price":179.6100},{"timestamp":1649228400,"price":159.3600},{"timestamp":1649232000,"price":100.0400},{"timestamp":1649235600,"price":77.0800},{"timestamp":1649239200,"price":91.5400},{"timestamp":1649242800,"price":97.2400},{"timestamp":1649246400,"price":93.9100},{"timestamp":1649250000,"price":92.9100},{"timestamp":1649253600,"price":99.4600},{"timestamp":1649257200,"price":126.2600},{"timestamp":1649260800,"price":165.5400},{"timestamp":1649264400,"price":178.4300},{"timestamp":1649268000,"price":171.0000},{"timestamp":1649271600,"price":132.4200},{"timestamp":1649275200,"price":109.1100},{"timestamp":1649278800,"price":98.6000}] 

以此类推

但我希望它像:

Prices: 55.00, 60.80, and so on
Timestamps: 1649192400, 1649196000, and so on

我怎样才能做到这一点?


编辑:我试过解析 JSON 但它所做的只是打印出来:

Table: 0x559d70fd0ac0

因为你打印了table,你需要打印table的内容,比如:

local t = cjson.decode(body)
print('Prices:', t.data.ee[1].timestamp)