Reputation: 1593
I am very new to Lua and am in need of assistance. I am trying to create a list of objects that have a name and a message. I need to be able to send JSON to my app like:
{{"name":"Joe","Message":"This is a test"),{....}}
From what I've read this could be accomplished with tables, but it does not seem to be working, what I have tried so far is
message = {}
messages = {}
message["name"] = "Joe"
message["message"] = "This is a Message"
messages["1"] = ?? <--- I don't know what to do here
Upvotes: 2
Views: 6786
Reputation: 26744
Assuming you fix your JSON code, which should probably look something like {{"name":"Joe","Message":"This is a test"},{....}}
, you can use the following code:
message = {name = "Joe", Message = "This is a Message"} -- capitalization in "Message" may matter
messages = {}
messages[1] = message
This is the same as:
message = {}
message["name"] = "Joe" -- or message.name = "Joe"
message["Message"] = "This is a Message" -- or message.Message = "...."
messages = {}
messages[1] = message -- the value of that element is a table
Note that I used [1]
and not ["1"]
, which are two different keys. Given your structure, you do want to use [1]
.
Upvotes: 8