【Defold】文字列をJSON形式に変換したい

2020/07/12Defold, DEVELOP

JSON API
https://www.defold.com/ref/json/

目次

要素を出力

local jsonstring = '{"persons":[{"name":"ponyo"},{"name":"pono"}]}'
local data = json.decode(jsonstring)
pprint(data)

結果

{
  persons = {
    1 = {
      name = ponyo,
    }
    2 = {
      name = pono,
    }
  }
}

要素を取り出す

local jsonstring = '{"persons":[{"name":"ponyo"},{"name":"pono"}]}'
local data = json.decode(jsonstring)
for key,v in pairs(data.persons[1]) do
    print("key=" .. key)
    print("value=" .. v)
end

結果

key=name
value=ponyo

お知らせ

Posted by kazupon