(Tcl) Load a JSON Array
The Chilkat JSON API requires the top-level JSON to be an object. Therefore, to load an array requires that it first be wrapped as an object.
load ./chilkat.dll
# Imagine we want to load this JSON array for parsing:
set jsonArrayStr "[{\"id\":200},{\"id\":196}]"
# First wrap it in a JSON object by prepending "{ "array":" and appending "}"
set sbJson [new_CkStringBuilder]
CkStringBuilder_Append $sbJson "{\"array\":"
CkStringBuilder_Append $sbJson $jsonArrayStr
CkStringBuilder_Append $sbJson "}"
set json [new_CkJsonObject]
CkJsonObject_Load $json [CkStringBuilder_getAsString $sbJson]
# Now we can get the JSON array
# jArray is a CkJsonArray
set jArray [CkJsonObject_ArrayAt $json 0]
# Do what you want with the JSON array...
# For example:
# jObjId is a CkJsonObject
set jObjId [CkJsonArray_ObjectAt $jArray 0]
puts [CkJsonObject_IntOf $jObjId id]
delete_CkJsonObject $jObjId
delete_CkJsonArray $jArray
delete_CkStringBuilder $sbJson
delete_CkJsonObject $json
|