在Lua中下载文件的最简单方法

我的 Lua 应用程序进行 http:request 操作,获取 JSON 格式的响应,将该 JSON 解码为表格。好的。

现在,我能够提取出 JSON 文件中的 url。

但是,我该如何将图像 url 保存到 Lua 文件中?是否有可用的库?

编辑:

如何在 Lua 中获取 JSON 文件:

json_lib = require('json')

local http = libs.net.http();

    local resp = http:request({
        method = "get",
        url = "http://developer.echonest.com/api/v4/artist/images?api_key=MY_API_KEY_HERE&name=zedd&format=json&results=1&start=0&license=unknown",
    });

    local json_full = resp.content;

    local str_decoded = json_lib.decode(json_full)

    function RecursiveSearch(aTable)

        for key, value in pairs(aTable) do --unordered search
            if(type(value) == "table") then
                print(key,value)
                RecursiveSearch(value)
            else
                --Do something with this.
                print(key,value)
            end
        end
    end
    RecursiveSearch(str_decoded)

我的 JSON 响应是:

{
                   "response":{
                      "status":{
                         "version":"4.2",
                         "code":0,
                         "message":"Success"
                      },
                      "start":0,
                      "total":20,
                      "images":[
                         {
                            "url":"http://userserve-ak.last.fm/serve/_/67419844/ZEDD.png",
                            "license":{
                               "type":"unknown",
                               "attribution":"n/a",
                               "url":"http://www.last.fm/music/ZEDD/+images"
                            }
                         }
                      ]
                   }
                }

因此,我想将带有艺术家图片的第一个 URL 保存在磁盘上。

点赞
用户258523
用户258523

在你的脚本末尾添加类似于以下内容(未经测试):

local imageresp = http:request({
    method = "get",
    url = url_from_decoded_json,
});

local imagefile = io.open("some_file", "w")
imagefile:write(resp.content)
imagefile:close()

并保留原始的 markdown 格式。

2013-07-31 03:01:51