Is there a JSON Decode Table Guru out there?

UPDATE: I figured it out but will leave this here for others. The solution was to index the table with a string index not a numeric one.

=============================================

I retrieve a json table online such as:
 
[
{
“item1”: “Some entry here”,
“item2”: “Some entry here”,
“item3”: “Some entry here”,
“item4”: “Some entry here”,
“item5”: “Some entry here”
},
{
“item1”: “Some entry here”,
“item2”: “Some entry here”,
“item3”: “Some entry here”,
“item4”: “Some entry here”,
“item5”: “Some entry here”
},
]

After using the Corona json.decode I can access item 3 in the 2nd table array like this:

local whatever = decodedArray[2][“item3”];

and #decodedArray returns the number of items in the table. HOWEVER,

I access another table which is like this:

{
“numFound”: 789,
“0”: {
“item1”: “Some entry here”,
“item2”: “Some entry here”,
“item3”: “Some entry here”,
“item4”: “Some entry here”,
“item5”: “Some entry here”
},
“1”: {
“item1”: “Some entry here”,
“item2”: “Some entry here”,
“item3”: “Some entry here”,
“item4”: “Some entry here”,
“item5”: “Some entry here”
},
}

#decodedArray returns 0 and I can’t access the individual arrays. I can see them in the debugger. decodedArray.numFound returns 789.

The main difference I see is the first decoded table starts and ends with “[]” and the second starts and ends with “{}” and the 2nd one I’m having problems with has a label before each table entry.

Any thoughts on accessing a table that starts end ends with braces?

This may interest you: http://developer.coronalabs.com/code/dkjson-mixed-indexing

This may interest you: http://developer.coronalabs.com/code/dkjson-mixed-indexing

the issue is that the first structure is an array thus the “[]”, the second one is a dictionary, thus the “{}”.

you can’t call #table on a table when used as a dictionary, you have to do the count manually. additionally, they aren’t indexed in the same fashion.

– this is fine for structure one because the first level is an array
local whatever = decodedArray[2][“item3”]

– this is how to index for structure two because the first level is a dictionary (note the quotes around “2”)
local whatever = decodedArray[“2”][“item3”]

cheers,
dmc

the issue is that the first structure is an array thus the “[]”, the second one is a dictionary, thus the “{}”.

you can’t call #table on a table when used as a dictionary, you have to do the count manually. additionally, they aren’t indexed in the same fashion.

– this is fine for structure one because the first level is an array
local whatever = decodedArray[2][“item3”]

– this is how to index for structure two because the first level is a dictionary (note the quotes around “2”)
local whatever = decodedArray[“2”][“item3”]

cheers,
dmc