I am playing with JSON for the first time and I am having a hard time trying to get the number of children inside an element. This is my json file:
{ "name": "Eyes of the Prophet", "format": { "orientation": "portrait" }, "pages": { "page1": { "width": 768, "height": 3072, "interface": { "layer1": { "file": "p1\_page1.png", "width": 768, "height": 1024, "x": 384, "y": 512 }, "layer2": { "file": "p1\_page2.png", "width": 768, "height": 1024, "x": 384, "y": 1536 }, "layer3": { "file": "p1\_page3.png", "width": 768, "height": 1024, "x": 384, "y": 2560 } } } } }
and this is the code I am using:
local json = require "json" local function jsonFile(filename ) local path = system.pathForFile(filename, system.DocumentsDirectory ) local contents local file = io.open( path, "r" ) if file then contents = file:read("\*a") print(contents) io.close(file) end return contents end local t = json.decode( jsonFile( "sample.json" ) ) print(#t.pages)
I would like to know how many “pages” I have (under the PAGES bracket), as well as how many “layers” I have inside a page. I tried all possible combinations without success to get the number of elements inside the table.
Any ideas what I am doing wrong?