JSON Starter

Hello, I am sorry I do not have any code as of yet… I plan to post a solution here so people can get started with JSON quickly.

In the mean time would people like so share/point me in the direction of decoding/encodeing JSON strings in lua. Wither there are ready made solutions or manual parsing is required…

EDIT:
Json library for codea:
http://pastebin.com/s2zdD5td

@Ruttyj That library wasn’t made by you, was it? I recognize it from Briarfox using it in AutoGist. Also, it’s not made for Codea, just generic Lua. And the term to use for it is module, not library. It would normally use the import function to load it.

Somewhere in gist I have JSON being used from a standard JSON library…but what are you trying to accomplish?

I would also wish to learn how to use JSON due to collecting data from a web site and use it in the app I have started. I have read some of the threads but as yet have not understood it.

Thanks in advance

Just grab the JSON library above. use json.decode(data from http request)
and to send json.encode()

@Briarfox I have got the http request to work in the end and data downloading but as yet still stuck on the decode side.

Json.decode(data)

But how do I read the decoded data ?

Thanks

local tbl = Json.decode(data)
The json data is placed into a table.

@Briarfox. Have tried your solution and still nothing.

Function setup()
http.request (“website”,yesdata)
End

Function yesdata(data,status,headers)
Print(status…“:”…data) – prints out the data from the website
Local tbl=json.decde(data)
Print (table.maxn(tbl)) – prints 0
End

Could you please point out where I have gone wrong

Thanks

@Jazmal well the use of table.maxn(tbl) may print zero, it depends on the type of table. Try removing the print and do this:

for i,j in pairs(tbl) do

print(i,j)

end

@Briarfox tried your solution and just printed one line

Table: 0x22215660

Altered the line to read

local (tbl) = json.decode(data,11)

Returns 42 items eg

  1. table: 0x1fd1ced0

Altered to json(data,12)

And it prints the info for only the first item

Regards

Yes each item returned with a table is a json return. Try this:

for i,j in pairs(tbl) do

for l,m in pairs(j) do
print(l,m)
end

end

Thanks for your help @Briarfox the solution works and it prints out all the infomation from the web site. All I have to do is learn how to manage the data and store it into an array I can use elsewhere in the app.

Thanks again for you help

Any time!

{“Sectors”:[{“X”:32,“Y”:-8,“Names”:[{“Text”:“Legend”}]},{“X”:-7,“Y”:-8,“Names”:[{“Text”:“Chtierabl”,“Lang”:“zh”}]},

The above is a sample taken from a http call.
I am trying to decode the above using json decoder. I have tried various things and the problem I am encountering is the 3rd item. Decodes the x and y but when it gets to the “Names” it just returns a table.

Names table: 0x1c096360

Any help with this would be helpful