Sample code for 30+ languages & platforms
PureBasic

Load a JSON Array

See more JSON Examples

The Chilkat JSON API requires the top-level JSON to be an object. Therefore, to load an array requires that it first be wrapped as an object.

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

    ; Imagine we want to load this JSON array for parsing:
    jsonArrayStr.s = "[{" + Chr(34) + "id" + Chr(34) + ":200},{" + Chr(34) + "id" + Chr(34) + ":196}]"

    ; First wrap it in a JSON object by prepending "{ "array":" and appending "}"
    sbJson.i = CkStringBuilder::ckCreate()
    If sbJson.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sbJson,"{" + Chr(34) + "array" + Chr(34) + ":")
    CkStringBuilder::ckAppend(sbJson,jsonArrayStr)
    CkStringBuilder::ckAppend(sbJson,"}")

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

    CkJsonObject::ckLoad(json,CkStringBuilder::ckGetAsString(sbJson))

    ; Now we can get the JSON array
    jArray.i = CkJsonObject::ckArrayAt(json,0)

    ; Do what you want with the JSON array...
    ; For example:
    jObjId.i = CkJsonArray::ckObjectAt(jArray,0)
    Debug Str(CkJsonObject::ckIntOf(jObjId,"id"))
    CkJsonObject::ckDispose(jObjId)

    CkJsonArray::ckDispose(jArray)



    CkStringBuilder::ckDispose(sbJson)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure