Sample code for 30+ languages & platforms
PureBasic Requires Chilkat v11.0.0+

JSON AppendArray2 Example

Demonstrates the AppendArray2 function.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkJsonArray.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(json,"{ " + Chr(34) + "name" + Chr(34) + ": " + Chr(34) + "John" + Chr(34) + ", " + Chr(34) + "marbles" + Chr(34) + ": 100 }")

    ;  Append an empty array named "xyz"
    jarr.i = CkJsonArray::ckCreate()
    If jarr.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckAppendArray2(json,"xyz",jarr)

    Debug CkJsonObject::ckEmit(json)
    ;  Expected output is:   {"name":"John","marbles":100,"xyz":[]}

    ;  Add elements to the array.
    CkJsonArray::ckAddStringAt(jarr,-1,"hello")
    CkJsonArray::ckAddIntAt(jarr,-1,256)

    Debug CkJsonObject::ckEmit(json)
    ;  Expected output is:  {"name":"John","marbles":100,"xyz":["hello",256]}


    CkJsonObject::ckDispose(json)
    CkJsonArray::ckDispose(jarr)


    ProcedureReturn
EndProcedure