I am trying to sync two databases using JSONItems. The JSON string below is what is returned from the server.
dim data as new JSONItem
data = js,Lookup("rSetups", nil)
if data <>nil then
for j as integer = 0 to data.Count-1
app.adb.SQLExecute "UPDATE Setups SET Item = '" + data.Value(j) + "', TimeStamp = '" + ts + "' WHERE Item = '" + data.Name(j) + "'"
next
end if
My code throws a JSONExceeption saying “JSONItem is not an array”. This is a very small test and there could be many more items. How can I get it to be treated as an array instead of an object?
data shows as {“FacCode”:“20466805”,“LastSync”:“2019-08-12 23:20:30”} in the debugger
the problem is that the JSON item is truly not an array.
Arrays are available in JSON and look like: { “myArray”: [ {“item1”:“value1”},{“item2”:“value2”},{“item3”:“value3”}] }
(note the square brackets)
So it sounds like you’re treating it like an array when you should treat it otherwise. Check out the structure of the JSONitem in the debugger. You might need to navigate from child to child in your processing loop rather than treat it as an array
Because it’s not data that you need in your loop, you need the JSONItem from the Child. Also you need to get the value from the JSONItem. You may want to use Lookup instead because if “Item” is not in the list row it will throw an exception.
Something like this (off the top of my head):
[code]if data <>nil then
for j as integer = 0 to data.Count-1
dim oChild as JSONItem = data.child(j)
app.adb.SQLExecute "UPDATE Setups SET TimeStamp = '" + ts + "' WHERE Item = '" + oChild.value("Item") + "'"
next
To build on Bob’s reply, “{}” denotes an object (the equivalent of a Xojo Dictionary), whereas “[]” denotes an array. Your JSON is an object within an object.