Hey guys, I’ve been working at this issue for almost two hours and have had no progress thus far. This is my first time really using the forums for help, so I hope this goes well!
I am using a third-party library (https://github.com/vsergeyev/corona-firebase) to use FireBase’s REST API as a form of backend. All I am doing is grabbing location data from the app, adding some text, and sending it to FireBase. That part of my app works perfectly with no errors.
I can grab the data fine through the REST API, when I print the raw JSON data it comes out perfectly. Yet, after I use JSON Decode, the data becomes nill and I can’t even use it.
I printed the raw JSON code and this comes out:
{ "-KQ9r2jNNYW6BmxA8pUC":{ "location":[10,-10], "text":"Hey Corona! Here’s some data for y’all!" } }
And then I tried taking that raw JSON string and decoding it, followed up by a simple print.
local dataPulled = (json.decode(event.response)) -- event.response IS the proper JSON data, I made sure or the first print wouldn't have worked print(dataPulled[0])
That prints a complete nil. I used a library from one of the Corona tutorials to try to print out the contents of dataPulled, and got:
--The print\_r library print\_r(dataPulled) --[[Returns: table: 0x7f89862ee080 { Aug 27 03:23:40.065 [-KQ9r2jNNYW6BmxA8pUC] =\> table: 0x7f89862ee080 { Aug 27 03:23:40.065 [location] =\> table: 0x7f8987acc370 { Aug 27 03:23:40.082 [1] =\> 10 Aug 27 03:23:40.083 [2] =\> -10 Aug 27 03:23:40.083 } Aug 27 03:23:40.083 [text] =\> "Hey Corona! Here’s some data for y’all!" Aug 27 03:23:40.083 } Aug 27 03:23:40.083 } ]]
How can I actually check the data?
Please let me know.
dataPulled[0] is nil because it doesn’t exist.
The contents of dataPulled are:
dataPulled.-KQ9r2jNNYW6BmxA8pUC (table)
dataPulled.-KQ9r2jNNYW6BmxA8pUC.location (table)
dataPulled.-KQ9r2jNNYW6BmxA8pUC.location[1] (number)
dataPulled.-KQ9r2jNNYW6BmxA8pUC.location[2] (number)
dataPulled.-KQ9r2jNNYW6BmxA8pUC.text - (string)
As the first table within dataPulled may have a different name each time, you’ll need to iterate over the contents of dataPulled to get at the data:
[lua]
local theData
for k,v in pairs( dataPulled ) do
theData = v
end
print ("LOCATION: “…theData.location[1]…” - "…theData.location[2])
print ("TEXT: "…theData.text)
[/lua]
@taha, use the following block to print the response.
[lua]
local printTable = function(table, stringPrefix)
if not stringPrefix then
stringPrefix = "### "
end
if type(table) == “table” then
for key, value in pairs(table) do
if type(value) == “table” then
print(stringPrefix … tostring(key))
print(stringPrefix … “{”)
printTable(value, stringPrefix … " ")
print(stringPrefix … “}”)
else
print(stringPrefix … tostring(key) … ": " … tostring(value))
end
end
end
end
local dataPulled = (json.decode(event.response))
printTable(dataPulled)
[/lua]
-Assif
Or you could use json.prettify():
print(json.prettify(event.response)) -- assumes event.response is a JSON string or a Lua table
dataPulled[0] is nil because it doesn’t exist.
The contents of dataPulled are:
dataPulled.-KQ9r2jNNYW6BmxA8pUC (table)
dataPulled.-KQ9r2jNNYW6BmxA8pUC.location (table)
dataPulled.-KQ9r2jNNYW6BmxA8pUC.location[1] (number)
dataPulled.-KQ9r2jNNYW6BmxA8pUC.location[2] (number)
dataPulled.-KQ9r2jNNYW6BmxA8pUC.text - (string)
As the first table within dataPulled may have a different name each time, you’ll need to iterate over the contents of dataPulled to get at the data:
[lua]
local theData
for k,v in pairs( dataPulled ) do
theData = v
end
print ("LOCATION: “…theData.location[1]…” - "…theData.location[2])
print ("TEXT: "…theData.text)
[/lua]
@taha, use the following block to print the response.
[lua]
local printTable = function(table, stringPrefix)
if not stringPrefix then
stringPrefix = "### "
end
if type(table) == “table” then
for key, value in pairs(table) do
if type(value) == “table” then
print(stringPrefix … tostring(key))
print(stringPrefix … “{”)
printTable(value, stringPrefix … " ")
print(stringPrefix … “}”)
else
print(stringPrefix … tostring(key) … ": " … tostring(value))
end
end
end
end
local dataPulled = (json.decode(event.response))
printTable(dataPulled)
[/lua]
-Assif
Or you could use json.prettify():
print(json.prettify(event.response)) -- assumes event.response is a JSON string or a Lua table