我的Lua应用程序发出http:请求,以JSON格式获取响应,将该JSON解码为表。行。
现在,我能够提取该JSON文件中的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"
}
}
]
}
}
所以我想在磁盘上保存第一个带有艺术家图片的网址。
答案 0 :(得分: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()
到你脚本的末尾。