I have a table created from a JSON string and am struggling to get at the information in it. I have read quite a few table tutorials but none seem to go deep enough.
Here is my JSON string -
{“results”:[{“offerDesc”:“30% off at whatever restaurant in town”,“offerURL”:“www. restaurant.com”},{“offerDesc”:“A bargain in liverpool”,“offerURL”:“www.anotherurl.co.uk”}]}
To get it into my table am using -
for k,v in pairs(response) do offers[k] = v end
But then if I go around myTable using pairs and printing the results to terminal am getting ‘table: 0x10b4c8770’
Ideally I would want to access myTable.offers like this -
offers[1].offerDesc
offers[2].offerDesc
Any ideas ?
Dave
Thats because there is a results variable inside your array.
Your format is like this
{
“results” : [
{
“offerDesc” : “30% off at whatever restaurant in town”,
“offerURL” : “www. restaurant.com”
},
{
“offerDesc” : “A bargain in liverpool”,
“offerURL” : “www.anotherurl.co.uk”
}
]
}
So as it is now, you will have to access the results as
offers.results[1].offerDesc
offers.results[2].offerDesc
if you want to access as offers[1].offerDesc, then your json should be
[{“offerDesc”:“30% off at whatever restaurant in town”,“offerURL”:“www. restaurant.com”},{“offerDesc”:“A bargain in liverpool”,“offerURL”:“www.anotherurl.co.uk”}]
Ok I understand now. It was confusing me. Thanks, Dave
Thats because there is a results variable inside your array.
Your format is like this
{
“results” : [
{
“offerDesc” : “30% off at whatever restaurant in town”,
“offerURL” : “www. restaurant.com”
},
{
“offerDesc” : “A bargain in liverpool”,
“offerURL” : “www.anotherurl.co.uk”
}
]
}
So as it is now, you will have to access the results as
offers.results[1].offerDesc
offers.results[2].offerDesc
if you want to access as offers[1].offerDesc, then your json should be
[{“offerDesc”:“30% off at whatever restaurant in town”,“offerURL”:“www. restaurant.com”},{“offerDesc”:“A bargain in liverpool”,“offerURL”:“www.anotherurl.co.uk”}]
Ok I understand now. It was confusing me. Thanks, Dave