parse and display json data

Hey guys, okay im probably biting off more than i can chew right now. Non programmer trying to learn lua and json at the same time.

Im loading in an external json file. I can download from the web, and load into my document directory no prob. But im not quite sure how to parse and display the data.

the json file itself is pretty simple:
< code>

[
{
“business”: “business 1”,
“location”: “address 1”,
“promotion”: “promo 1”
},
{
“business”: “business 2”,
“location”: “address 2”,
“promotion”: “promo 2”
},
{
“business”: “business 3”,
“location”: “address 3”,
“promotion”: “promo 3”
}
]

< /code>

im not sure if thats even the best or most efficient way to write json. I found http://www.jsoneditoronline.org/ from another post here.

how do i take this data and use it?

I ultimately want to take the json data and put it into tableview, but baby steps first!

thanks corona gurus!
-K [import]uid: 88004 topic_id: 25742 reply_id: 325742[/import]

:confused: code didnt highlight? [import]uid: 88004 topic_id: 25742 reply_id: 104082[/import]

Let’s say you have downloaded your json data and that you’ve stored it and read it in and it’s in some variable, that I will call downloadedJSONdata and it holds the JSON content.

local json = require("json")  
  
local someTable = json.decode(downloadedJSONdata)  
  

Now you have a lovely Lua table where each member contains data from the JSON string:

  
print(sometable[1].business) -- prints "business 1"  
print(sometable[1].location) -- prints "address 1"  
print(sometable[3].promotion) -- prints "promo 3"  
  

You could also reference it like:

sometable[1][“business”]

if you wanted too.

The reason your < code> didn’t work is you have to take the space out between the angle bracket and the word code or the /code. It should look more like this:

<code> and </code>

[import]uid: 19626 topic_id: 25742 reply_id: 104110[/import]

http://blog.anscamobile.com/2011/08/tutorial-exploring-json-usage-in-corona/

There is an in-built “json” library in corona.

EDIT : Ah! I see I’m a bit late… and a lot less informative.
:smiley: [import]uid: 64174 topic_id: 25742 reply_id: 104114[/import]

thank you so much for your responses! robmiracle, what you showed me was literally a miracle hah. I was confused with how to call the value i wanted. I was trying to call them like:

 someTable.location.blahblah 

@Satheesh - no worries that tut is great! It was a huge help in teaching me how to load the json file. just couldnt understand how to pull the values i wanted.

cheers!
[import]uid: 88004 topic_id: 25742 reply_id: 104262[/import]