JSON

Reading data from MySQL database, using PHP, I get the following (using the http request in Corona) data from the table: [[“Alex”,35],[“John”,28]]. Unfortunately it comes as string (so I have to read myTable[1]), and I cannot get the reality (in this example, 2 records, with 2 fields each).

I tried the JSON.lua but the only thing it is doing is something like [""["…

What is the best way to get the results from my PHP script as a Lua array/table, instead the simple text? If someone is kind enough to check the returning from my php script, just try it at http://www.ppgplaces.com/getPilots.php

thanks a lot, Alex [import]uid: 4883 topic_id: 2584 reply_id: 302584[/import]

Hi Alex,

Here’s a quick and dirty version that parses your PHP output into a table and and dumps its contents to the terminal. I’m using this Json library:

http://www.chipmunkav.com/downloads/Json.lua

http = require("socket.http")  
crypto = require("crypto")  
ltn12 = require("ltn12")  
url = require("socket.url")  
require("Json")  
 local json = {}  
   
local response = {}  
function dump(o)  
 if type(o) == 'table' then  
 local s = '{ '  
 for k,v in pairs(o) do  
 if type(k) ~= 'number' then k = '"'..k..'"' end  
 s = s .. '['..k..'] = ' .. dump(v) .. ','  
 end  
 return s .. '} '  
 else  
 return tostring(o)  
 end  
end  
  
   
local r, c, h = http.request {  
 url = "http://www.ppgplaces.com/getPilots.php",  
 sink = ltn12.sink.table(response)  
}  
   
json = Json.Decode(table.concat(response,''))  
  
local path = system.pathForFile("r.txt", system.DocumentsDirectory)  
local file = io.open (path, "w")  
  
print(dump(response))   

Tim [import]uid: 8196 topic_id: 2584 reply_id: 7480[/import]

Btw, my code is based on ehsmeng’s over here:

http://developer.anscamobile.com/forum/2010/06/06/post-json-data-and-receive-json-data-example [import]uid: 8196 topic_id: 2584 reply_id: 7481[/import]

How do I get the records? As you can see below, the “new” table returned 7073 records, which is not reality. I tried to:

local myTable = dump(response)
print(#myTable) --RETURNED 7073!!
print(myTable[1][1])
print (myTable[1])

nothing worked… [import]uid: 4883 topic_id: 2584 reply_id: 7482[/import]

Just a side-question…

I’m using this “dkjson” module from
http://www-users.rwth-aachen.de/David.Kolf/json-lua

after reading the following comparison:
http://lua-users.org/wiki/JsonModules

Any good/bad experiences with any of the available json-modules?

-Frank. [import]uid: 8093 topic_id: 2584 reply_id: 7497[/import]