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
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)
-- wait for a GET request to terminate
await(client:get("/json/"))
-- decode the response as a JSON string or throw an error
local data = json.decode(client.response and client.response.content or error(net.error))
-- use the needed response fields
print("You are located in "..data.country.." near "..data.city)