I’m having trouble adding multiple values to a table. I think it is just a syntax issue but would appreciate any suggestions.
I have the following table defined:
local nameData = {category,price}
- I grab the Json data (coming from a mysql database)
myData = json.decode(event.response)
myData looks like this:
{ “data”:[{“price”:125,“duration”:300,“currency”:“USD”,“category”:“Wilderness Training”},{“price”:125,“duration”:300,“currency”:“USD”,“category”:“Camping”}, etc
- I am trying to insert the myData into nameData. The following works but it does not insert a new row in the table and simply adds the last data set.
for i=1, #myData.data do
if myData.data[i].category ~= nil then
for k,v in pairs( myData) do
nameData[i].category=myData.data[i].category
nameData[i].price=myData.data[i].price
end
end
end
The result is {category=blah,price=111} but only a single row containing the last record in the json data set.
- Table.insert only allows for one value. I tried the following but none work:
This adds two new rows
table.insert( nameData, myData.data[i].category)
table.insert(nameData, myData.data[i].price)
This errors out
table.insert(nameData, myData.data[i].category, myData.data[i].price)
And so does this
table.insert(nameData, {myData.data[i].category, myData.data[i].price})
I’ve tried 4 or 5 other ways but keep hitting a wall.
Any suggestions? Thanks