2

我的 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 保存在磁盘上。

4

1 回答 1

5

添加类似(未经测试)的内容:

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

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

到你的脚本的末尾。

于 2013-07-31T03:01:51.687 回答