Any wong with this code, I want to download the file from website and read it

Any wong with this code, I want to download the file from website and read it

local http = require("socket.http")  
local json = require("json")  
local jsonfile ={}  
  
local jsonFile = function( theFile,base )  
   
 print("theFile:"..theFile)  
  
 if not base then   
 base = system.ResourceDirectory;   
 end  
   
 local path = system.pathForFile( theFile, base )  
   
 local contents  
   
 local file = io.open( path, "r" )  
 if file then  
 --print("success")   
 contents = file:read( "\*a" )  
 io.close( file )   
 end  
  
 return contents  
end  
  
function downloadFile(data)  
 -- Create local file for saving data  
  
 local path = system.pathForFile(data, system.DocumentsDirectory )  
 local myFile = io.open( path, "w+b" )   
  
 native.setActivityIndicator( true ) -- show busy  
   
 -- Request remote file and save data to local file  
 http.request{   
 url = "http://apps4market.com/"..data,   
 sink = ltn12.sink.file(myFile),  
 }   
  
 native.setActivityIndicator( false )   
  
 return true   
end  
  
downloadFile("level.json")  
print("decode to JSON file")  
  
jsonfile = json.decode( jsonFile( "level.json", system.ResourceDirectory ) )  
  
print("No of Record:"..#jsonfile)  
  

[import]uid: 22631 topic_id: 27156 reply_id: 327156[/import]

Hey Raymond,

Yeah, you’re downloading to system.DocumentsDirectory but then trying to read from system.ResourceDirectory.

Change line 51 to;
[lua]jsonfile = json.decode( jsonFile( “level.json”, system.DocumentsDirectory ) )[/lua]
That will fix it.

Peach :slight_smile: [import]uid: 52491 topic_id: 27156 reply_id: 110258[/import]