Blitz3D+ Command Reference

JsonArrayRemove ( json,index )

Parameters

json - JSON array handle to modify

index - position of the element to remove, 0 to JsonCount(json)-1

Description

Removes an existing array element and returns true.

The hero drinks the potion in slot 2 and it vanishes from the inventory: the element at the index is deleted, everything after it shifts one place left, and JsonCount shrinks by one.

The index must address an existing element. Unlike JsonObjectRemove - which quietly returns false for a missing key - an out-of-range array index here is a programming error that stops the program, so there is no false result to test for; the call returns true whenever it returns at all. Range-check against JsonCount first when the index comes from anywhere untrusted.

Mind the shift when removing inside a loop: after removing index i, the old element i+1 is now at i. Loop backwards, or do not advance the index after a removal. Copies of the removed element fetched earlier with JsonArrayGet stay valid - they are independent. Calling JsonArrayRemove on a non-array is a programming error.

For the full rules, see the Application Data language reference.

Requires Extended mode.

See also: JsonArrayClear, JsonArrayInsert, JsonArrayGet, JsonObjectRemove, JsonCount.

Example

; JsonArrayRemove Example
; -----------------------
; Requires Extended mode.

q$=Chr(34)  ; Double-quote character, for building JSON text

; The hero's inventory - the potion sits in slot 2
inventory=ParseJson("["+q+"sword"+q+","+q+"shield"+q+","+q+"potion"+q+"]")
Print "Before drinking: "+JsonStringify(inventory,0)
Print "Item count: "+JsonCount(inventory)
Print ""

; The hero drinks the potion - JsonArrayRemove deletes slot 2
; and returns true on success
ok=JsonArrayRemove(inventory,2)
Print "JsonArrayRemove(inventory,2) returned "+ok

Print "After drinking:  "+JsonStringify(inventory,0)
Print "Item count: "+JsonCount(inventory)

FreeJson inventory

Print ""
Print "Press any key to close the example"
WaitKey

End

Index