Hi all,
i’m trying to read json from a string that is downloaded from the web everytime i start my application, but i’m stuck on the json part.
I’ve summarized my problem into this small script:
the declaration of the local var t in comments is working, but when it comes from a string (what my larger app will do), then it crashes.
The error i get is this: Runtime error: /Users/ruben/Desktop/main.lua:16: attempt to index field ‘name2’ (a nil value)
does anybody knows how to convert a string into json ?
Thanks a lot
require(“json”)
local t = ‘{ [“name1”] = “value1”,[“name2”] = {1, false, true, 23.54, “a \021 string”}}’
–local t = {
– [“name1”] = “value1”,
– [“name2”] = {1, false, true, 23.54, “a \021 string”}
–}
local json = Json.Encode (t)
print (json)
–> {“name1”:“value1”,“name3”:null,“name2”:[1,false,true,23.54,“a \u0015 string”]}
local t = Json.Decode(json)
print(t.name2[4])
–> 23.54 [import]uid: 13464 topic_id: 4928 reply_id: 304928[/import]