在Lua中拆分桌子

发布于 2025-01-19 11:26:28 字数 1681 浏览 1 评论 0原文

我有一个脚本,可以为我提供一个网站API的时间戳和价格表,我想分开将它们分开,分别分别为时间戳。

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

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

I have a script which gives me a table of timestamps and prices from a website API, I want to split them apart into timestamp and prices separately.

My code to get the prices and timestamps

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

Output:

{"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}] 

And so on.

But I want it to be like:

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

How can I make it like that?


Edit: I have tried parsing JSON but all it did was print out:

Table: 0x559d70fd0ac0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两个我 2025-01-26 11:26:28

因为您打印了表格,所以需要打印表格的内容,例如:

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

Because you printed the table, you need print the content of the table, something like:

local t = cjson.decode(body)
print('Prices:', t.data.ee[1].timestamp)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文