2013-07-30 41 views
2

我的Lua應用程序創建一個http:request,以JSON格式獲取響應,將該JSON解碼爲一個表格。好。在Lua中下載文件的最簡單的方法

現在,我能夠提取該JSON文件中的URL。

但是,如何將圖像url保存到Lua中的文件?有沒有圖書館?

編輯:

如何採取JSON文件中的Lua:

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

你是說保存實際圖像本身還是隻保存url文本鏈接? – greatwolf

+0

我想保存圖像本身。我已經完成的url文本。感謝您的關注。 – MatheusLPS

+0

您必須已經使用庫來獲取json文檔,否?不只是使用它來下載圖像文件的工作? –

回答

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() 

到腳本的結尾。

+0

感謝Etan Reisner提供的最後一個答案。對不起,我返回的延遲。 問題是,我使用的Windows程序有一個不好的二進制數據處理。它只保存了1kb(損壞的)圖像文件。 我報告了錯誤,開發人員更新了測試版程序,問題解決了。現在我可以用你的上述想法下載圖像。 再見 – MatheusLPS