メインコンテンツまでスキップ

json.decode()

JSON テキストを Lua の値にデコードします。

シグネチャ​

json.decode(text: string): any

パラメータ​

パラメータ型説明
textstring解析する JSON テキスト

戻り値

デコードされた値。オブジェクトや配列の場合はテーブル、その他は文字列、数値、真偽値、nil です。

説明​

無効な JSON はエラーになります。サーバーが HTML のエラーページなど別のものを返す可能性がある場合は、ネットワークからの入力を pcall() で囲んでください。

例​

local response = Request("https://api.example.com/user/1"):get()
local ok, user = pcall(function()
return json.decode(response:getData())
end)
if ok then
toast("Hello " .. user.name)
end