Sample code for 30+ languages & platforms
Lua

JSON: Nested Array

See more JSON Examples

Here we have a JSON object that contains nested arrays. This example demonstrates how to access the contents of the nested arrays.
{
 "numbers" : [ 
    ["even", 2, 4, 6, 8], 
    ["prime", 2, 3, 5, 7, 11, 13] 
  ] }

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 = "{ \"numbers\" : [ [\"even\", 2, 4, 6, 8], [\"prime\", 2, 3, 5, 7, 11, 13] ] }"

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

-- Get the value of the "numbers" object, which is an array that contains JSON arrays.
local outerArray = json:array_of("numbers")
if not json.last_method_success then
    print("numbers array not found.")
    return
end

local numArrays = outerArray.size

for i = 0, numArrays - 1 do
    local innerArray = outerArray:array_at(i)

    -- The first item in the innerArray is a string
    print(innerArray:string_at(0) .. ":")

    local numInnerItems = innerArray.size
    for j = 1, numInnerItems - 1 do
        print("  " .. innerArray:int_at(j))
    end
end