Sample code for 30+ languages & platforms
PureBasic

JSON AppendObject2 Example

Demonstrates the AppendObject2 function.

Chilkat PureBasic Downloads

PureBasic
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 object named "addr"
    jObj.i = CkJsonObject::ckCreate()
    If jObj.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckAppendObject2(json,"addr",jObj)

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

    ; Add members to the object.
    CkJsonObject::ckAppendString(jObj,"street","1200 Elm St.")
    CkJsonObject::ckAppendString(jObj,"city","Springfield")
    CkJsonObject::ckAppendInt(jObj,"zip",60606)

    Debug CkJsonObject::ckEmit(json)
    ; Expected output is:  {"name":"John","marbles":100,"addr":{"street":"1200 Elm St.","city":"Springfield","zip":60606}}


    CkJsonObject::ckDispose(json)
    CkJsonObject::ckDispose(jObj)


    ProcedureReturn
EndProcedure