MemberByIndex - cJsonObject

Get a member object its index. Can be used for enumerating object properties and to access array items.

Type: Function

Return Data Type: Handle

Parameters: Integer iIndex

ParameterDescription
iIndexArray index of member


Return Value

Handle of JSON object


Syntax
Function MemberByIndex Integer iIndex Returns Handle

Call: Get MemberByIndex iIndex to HandleVariable


Description

Get a member object its index. Can be used for enumerating object properties and to access array items.

The JsonType and MemberJsonType functions can be used to determine the type of a specific node.


Traversing Structure

A JSON document should be seen as a tree of objects, arrays and simple type nodes as leaves. A cJsonObject instance represents a single node in that tree and there are several API's available to traverse the tree. The JsonType and MemberJsonType functions can be used to determine the type of a specific node.

The MemberCount function returns the number of members on an object or an array and using Member or MemberByIndex members can be enumerated. When Member or MemberByIndex is used to obtain a handle to a member node, this handle needs to be destroyed manually. Note that destroying a member handle does not actually change the JSON document.

Sample

The example below enumerates a JSON object showing information about its members. It outputs the following:
Member 'name' of type 6
Member 'details' of type 4

Handle hoJson hoMember
Integer iMem iType
String sName
Boolean bSuccess

Get Create (RefClass(cJsonObject)) to hoJson
Get ParseString of hoJson '{"name" : "John", "details" : {"age" : 31, "male" : true}}' to bSuccess

If (bSuccess) Begin
    For iMem from 0 to (MemberCount(hoJson) - 1)
        Get MemberNameByIndex of hoJson iMem to sName
        Get MemberByIndex of hoJson iMem to hoMember
        Get JsonType of hoMember to iType
        
        Showln (SFormat("Member '%1' of type %2", sName, iType))
        
        Send Destroy of hoMember
    Loop
End
Begin
    Send ReportParseError of hoJson
End

Send Destroy of hoJson

See Also

MemberNameByIndex | MemberCount