Sample code for 30+ languages & platforms
PureBasic

Example: JsonObject.NewObjectOf method

Demonstrates the NewObjectOf method.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

    car.s = "{" + Chr(34) + "make" + Chr(34) + ":" + Chr(34) + "Toyota" + Chr(34) + "," + Chr(34) + "model" + Chr(34) + ":" + Chr(34) + "Camry" + Chr(34) + "," + Chr(34) + "year" + Chr(34) + ":2022," + Chr(34) + "color" + Chr(34) + ":" + Chr(34) + "silver" + Chr(34) + "," + Chr(34) + "engine" + Chr(34) + ":{" + Chr(34) + "type" + Chr(34) + ":" + Chr(34) + "inline-4" + Chr(34) + "," + Chr(34) + "fuel" + Chr(34) + ":" + Chr(34) + "gasoline" + Chr(34) + "," + Chr(34) + "horsepower" + Chr(34) + ":203}," + Chr(34) + "features" + Chr(34) + ":[" + Chr(34) + "bluetooth" + Chr(34) + "," + Chr(34) + "backup camera" + Chr(34) + "," + Chr(34) + "adaptive cruise control" + Chr(34) + "," + Chr(34) + "lane assist" + Chr(34) + "]," + Chr(34) + "isElectric" + Chr(34) + ":false}"

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

    CkJsonObject::ckUpdateString(json,"test","abc")

    success = CkJsonObject::ckNewObjectOf(json,"car",car)
    If success = 0
        Debug CkJsonObject::ckLastErrorText(json)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    CkJsonObject::setCkEmitCompact(json, 0)
    Debug CkJsonObject::ckEmit(json)

    ; Result:

    ; {
    ;   "test": "abc",
    ;   "car": {
    ;     "make": "Toyota",
    ;     "model": "Camry",
    ;     "year": 2022,
    ;     "color": "silver",
    ;     "engine": {
    ;       "type": "inline-4",
    ;       "fuel": "gasoline",
    ;       "horsepower": 203
    ;     },
    ;     "features": [
    ;       "bluetooth",
    ;       "backup camera",
    ;       "adaptive cruise control",
    ;       "lane assist"
    ;     ],
    ;     "isElectric": false
    ;   }
    ; }


    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure