json.decode(string)
Decode a JSON string to a Lua value, following those rules :
JSON | Lua value type |
---|---|
"string" |
"string" |
number |
number |
"true" / "false" |
boolean |
object {} |
table |
array [] |
table (integer keys) |
null |
"null" |
Parameters
string
JSON string to be decoded.
Return value
Returns the corresponding Lua value or anil
value in case of error.
Example
--
-- LuaRT geo.lua example
-- Find the location where you currently are
--
local net = require "net"
local json = require "json"
-- use the free IP-API localization web API
local url = "http://ip-api.com"
local client = net.Http(url)
-- make a GET request, returning the response as a JSON string
-- decoded using json.decode() function
local uri = "/json/"
local response = json.decode(client:get(uri))
-- parse the response (a Lua table)
print("You are located in "..response.country.." near "..response.city)