http://docs.coronalabs.com/daily/api/library/network/download.html
like this , i wanna load a json file and open it , how to do it ??
for example,
how to download this data insert my code ??
http://docs.coronalabs.com/daily/api/library/network/download.html
like this , i wanna load a json file and open it , how to do it ??
for example,
how to download this data insert my code ??
If you’re calling an API like graph.facebook.com/4 then you really want to use network.request() not network.download(). The network.download() will download the results and put it in a text file in your system.DocumentsDirectory (or wherever you directed it to). You would then have to use the File IO system to open that file, read it in and then convert the data you read from JSON to a Lua table.
If you do network.request(), the JSON data will be in memory and you can skip the whole file IO process. In your listener function that handles the network.request() events, when you get event for when the job ended, you will have the JSON data in a variable called event.response. Simply do:
myDataTable = json.decode(event.response)
to convert the JSON data into a table called myDataTable (you do have to a:
local json = require(“json”)
before calling the json libraries the first time.
thx ~~ i did it
like this
local json = require “json”
local function networkListener( event )
if ( event.isError ) then
print( “Network error!” )
else
--print ( "RESPONSE: " … event.response )
myDataTable = json.decode(event.response)
data = json.encode(myDataTable)
print ( data )
end
end
– Access Google over SSL:
network.request( “https://graph.facebook.com/4”, “GET”, networkListener )
If you’re calling an API like graph.facebook.com/4 then you really want to use network.request() not network.download(). The network.download() will download the results and put it in a text file in your system.DocumentsDirectory (or wherever you directed it to). You would then have to use the File IO system to open that file, read it in and then convert the data you read from JSON to a Lua table.
If you do network.request(), the JSON data will be in memory and you can skip the whole file IO process. In your listener function that handles the network.request() events, when you get event for when the job ended, you will have the JSON data in a variable called event.response. Simply do:
myDataTable = json.decode(event.response)
to convert the JSON data into a table called myDataTable (you do have to a:
local json = require(“json”)
before calling the json libraries the first time.
Hi,
Sorry to revive this thread, but I figured it would be better than starting a new one. I’m pretty new to Corona, but I’m getting the hang of it… so please bear with my newbishness =]
I’m trying to do the same thing the OP is, but I’m having trouble accessing the table after it has been decoded.
This is what I have:
local json = require ("json"); local jsonTable = {}; local function networkListener(event) local data = event.response; local jsonTable = json.decode(data); print(jsonTable); end local remoteFeed = "http://api.trakt.tv/search/movies.json/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*?query=batman&limit=1"; network.request(remoteFeed, "GET", networkListener);
if I print(data) I can see that the information is grabbed, but printing(jsonTable) just gives me “Table:09B7547”. The numbers/letters are always random. If I print(jsonTable.title), the console just gives me “nil”.
How do I access each row of the table? Like if I wanted to store “Title” into a variable, how would I tell Corona that I want that?
Thanks so much guys!
thx ~~ i did it
like this
local json = require “json”
local function networkListener( event )
if ( event.isError ) then
print( “Network error!” )
else
--print ( "RESPONSE: " … event.response )
myDataTable = json.decode(event.response)
data = json.encode(myDataTable)
print ( data )
end
end
– Access Google over SSL:
network.request( “https://graph.facebook.com/4”, “GET”, networkListener )
json.decode() returns a table. When you try and print a table, you just get it’s memory address. You need a table printing function. See:
http://coronalabs.com/blog/2014/09/02/tutorial-printing-table-contents/
You should print out the table’s contents and see what’s there. also print out the event.response value and make sure you’re getting valid JSON back from the server.
Rob
Ah, that makes since. printing the event does give me the proper JSON information, that’s why I thought printing the table would give me something similar. Thanks again, I’m sure I’ll have more question as I follow this crazy road! =]
Hi,
Sorry to revive this thread, but I figured it would be better than starting a new one. I’m pretty new to Corona, but I’m getting the hang of it… so please bear with my newbishness =]
I’m trying to do the same thing the OP is, but I’m having trouble accessing the table after it has been decoded.
This is what I have:
local json = require ("json"); local jsonTable = {}; local function networkListener(event) local data = event.response; local jsonTable = json.decode(data); print(jsonTable); end local remoteFeed = "http://api.trakt.tv/search/movies.json/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*?query=batman&limit=1"; network.request(remoteFeed, "GET", networkListener);
if I print(data) I can see that the information is grabbed, but printing(jsonTable) just gives me “Table:09B7547”. The numbers/letters are always random. If I print(jsonTable.title), the console just gives me “nil”.
How do I access each row of the table? Like if I wanted to store “Title” into a variable, how would I tell Corona that I want that?
Thanks so much guys!
json.decode() returns a table. When you try and print a table, you just get it’s memory address. You need a table printing function. See:
http://coronalabs.com/blog/2014/09/02/tutorial-printing-table-contents/
You should print out the table’s contents and see what’s there. also print out the event.response value and make sure you’re getting valid JSON back from the server.
Rob
Ah, that makes since. printing the event does give me the proper JSON information, that’s why I thought printing the table would give me something similar. Thanks again, I’m sure I’ll have more question as I follow this crazy road! =]