JsonArrayGet ( json,index )
Parameters
|
json - JSON array handle index - element position, 0-based |
Description
|
Returns an owned copy of an array element, or 0 when the index is out of range. This is how you read items out of a loaded inventory, quest log or high-score list. The returned handle is a fully independent deep copy: it stays valid even if you later change, clear or free the parent array, and freeing it never touches the parent. You own it - release it with FreeJson. Any out-of-range index, including a negative one, returns 0 quietly instead of raising an error - so optional reads need no ceremony, and since FreeJson 0 is a safe no-op you can free the result unconditionally. This is deliberately different from the mutators (JsonArraySet, JsonArrayRemove), which treat a bad index as a programming error. The usual loop runs index 0 to JsonCount(json)-1, fetching, reading and freeing each element. Because every get copies, hoist the fetch out of inner loops when an element is large. Calling JsonArrayGet on a non-array is a programming error. For the full rules, see the Application Data language reference. Requires Extended mode. See also: JsonCount, JsonArraySet, JsonArrayAppend, JsonObjectGet, FreeJson. |
Example
; JsonArrayGet Example ; -------------------- ; Requires Extended mode. q$=Chr(34) ; Double-quote character, for building JSON text ; The hero's inventory from a loaded save-game inventory=ParseJson("["+q+"sword"+q+","+q+"shield"+q+","+q+"potion"+q+"]") Print "Inventory: "+JsonStringify(inventory,0) Print "" ; JsonArrayGet returns an OWNED copy of each element For i=0 To JsonCount(inventory)-1 item=JsonArrayGet(inventory,i) Print "Slot "+i+": "+JsonText(item) FreeJson item Next ; An out-of-range index returns zero instead of a handle missing=JsonArrayGet(inventory,99) Print "" Print "JsonArrayGet(inventory,99) = "+missing+" (0 means no such element)" FreeJson missing ; FreeJson 0 is safe FreeJson inventory Print "" Print "Press any key to close the example" WaitKey End
Index