Sample code for 30+ languages & platforms
C

Using JSON StringOf with Non-String Members

See more JSON Examples

If a JSON member is a boolean, integer, or null, using StringOf will return its string representation.

Chilkat C Downloads

C
#include <C_CkJsonObject.h>

void ChilkatSample(void)
    {
    HCkJsonObject json;
    const char *a;
    const char *b;
    const char *c;
    int ival;
    BOOL bval;
    BOOL hasNull;

    //  Create JSON with members of different data types.
    json = CkJsonObject_Create();

    CkJsonObject_UpdateInt(json,"a",123);
    CkJsonObject_UpdateBool(json,"b",TRUE);
    CkJsonObject_UpdateNull(json,"c");

    CkJsonObject_putEmitCompact(json,FALSE);
    printf("%s\n",CkJsonObject_emit(json));

    //  Resulting JSON:
    //  {
    //    "a": 123,
    //    "b": true,
    //    "c": null
    //  }

    a = CkJsonObject_stringOf(json,"a");
    printf("%s\n",a);

    b = CkJsonObject_stringOf(json,"b");
    printf("%s\n",b);

    c = CkJsonObject_stringOf(json,"c");
    printf("%s\n",c);

    //  Output

    //  123
    //  true
    //  null

    //  If you want to get the integer, boolean, or null value
    //  you need to use the methods matching the data type
    ival = CkJsonObject_IntOf(json,"a");
    bval = CkJsonObject_BoolOf(json,"b");
    hasNull = CkJsonObject_IsNullOf(json,"c");


    CkJsonObject_Dispose(json);

    }