JsonCount ( json )
Parameters
| json - JSON array or object handle |
Description
|
Returns the number of elements in an array or properties in an object. This is the loop bound of the JSON family. On an array it counts elements, giving you the range for JsonArrayGet: indices 0 to JsonCount(json)-1. On an object it counts properties, giving you the range for JsonObjectKey - walk every key of a save file without knowing its layout upfront. JsonCount only accepts containers. Calling it on a scalar - a string, number, Boolean or null - is a programming error that stops the program; when the data is untrusted, check JsonKind for JSON_ARRAY or JSON_OBJECT first. An empty array or object counts 0, which is a perfectly normal answer, not an error. For the full rules, see the Application Data language reference. Requires Extended mode. See also: JsonKind, JsonArrayGet, JsonObjectKey, CreateJsonArray, CreateJsonObject. |
Example
; JsonCount Example ; ----------------- ; Requires Extended mode. q$=Chr(34) ; Double-quote character, for building JSON text ; A save-game with an inventory array inside an object src$="{"+q+"player"+q+":"+q+"Aria"+q+","+q+"level"+q+":7," src=src+q+"inventory"+q+":["+q+"sword"+q+","+q+"shield"+q+","+q+"potion"+q+"]}" save=ParseJson(src) ; On an object, JsonCount is the number of properties Print "Save-game: "+JsonStringify(save,0) Print "Fields in the save object: "+JsonCount(save) ; On an array, JsonCount is the number of elements inventory=JsonObjectGet(save,"inventory") Print "Items in the inventory: "+JsonCount(inventory) Print "" Print "Aria carries "+JsonCount(inventory)+" items." FreeJson inventory FreeJson save Print "" Print "Press any key to close the example" WaitKey End
Index