Sample code for 30+ languages & platforms
Lua

JSON: Nested Objects

See more JSON Examples

Here we have a JSON object that contains nested JSON objects. This example demonstrates how to access the contents of the nested objects.
{
  "name": "donut",
  "image":
    {
    "fname": "donut.jpg",
    "w": 200,
    "h": 200
    },
  "thumbnail":
    {
    "fname": "donutThumb.jpg",
    "w": 32,
    "h": 32
    }
}

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

local json = chilkat.JsonObject.new()

-- This is the above JSON with whitespace chars removed (SPACE, TAB, CR, and LF chars).
-- The presence of whitespace chars for pretty-printing makes no difference to the Load
-- method.
local jsonStr = "{\"name\": \"donut\",\"image\":{\"fname\": \"donut.jpg\",\"w\": 200,\"h\": 200},\"thumbnail\":{\"fname\": \"donutThumb.jpg\",\"w\": 32,\"h\": 32}}"

local success = json:load(jsonStr)
if not success then
    print(json.last_error_text)
    return
end

-- Get the "image" object.
local imageObj = chilkat.JsonObject.new()
json:object_of2("image", imageObj)

print("image: fname=" .. imageObj:string_of("fname") .. ", width=" .. imageObj:int_of("w") .. ", height=" .. imageObj:int_of("h"))

-- Get the "thumbnail" object.
local thumbObj = chilkat.JsonObject.new()
json:object_of2("thumbnail", thumbObj)

print("thumbnail: fname=" .. thumbObj:string_of("fname") .. ", width=" .. thumbObj:int_of("w") .. ", height=" .. thumbObj:int_of("h"))