Source Code File 12-06_recursive

12-06_recursive.c

#include <stdio.h>
#include <stdlib.h>
#include <json-c/json.h>

void parse_json_object(
        json_object *jobj,
        struct lh_entry *ent,
        int indent)
{
    json_object *keyname,*jelement;
    enum json_type type;
    char *key;
    const char *jstring;
    int jint,jbool,elements,x;
    struct json_object *val;
    struct lh_entry *e;

    while(ent)
    {
        key = (char *)ent->k;
        val = (struct json_object *)ent->v;
        json_object_object_get_ex(jobj, key, &keyname);
        printf("%*c'%s' type is ",indent*4,' ',key);
        type = json_object_get_type(val);
        switch(type)
        {
            case json_type_array:
                elements = json_object_array_length(keyname);
                printf("array with %d elements:",elements);
                for( x=0; x<elements; x++)
                {
                    jelement=json_object_array_get_idx(
                            keyname,
                            x
                            );
                    printf(" %d",json_object_get_int(jelement));
                }
                putchar('\n');
                break;
            case json_type_boolean:
                jbool = json_object_get_boolean(keyname);
                printf("boolean, value: %s\n",
                        (jbool?"TRUE":"FALSE")
                    );
                break;
            case json_type_double:
                puts("Double");
                break;
            case json_type_int:
                jint = json_object_get_int(keyname);
                printf("integer, value: %d\n",jint);
                break;
            case json_type_null:
                puts("Null");
                break;
            case json_type_object:
                puts("JSON object:");
                e = json_object_get_object(keyname)->head;
                parse_json_object(keyname,e,indent+1);
                break;
            case json_type_string:
                jstring = json_object_get_string(keyname);
                printf("string, value: %s\n",jstring);
                break;
            default:
                puts("Unrecognized");
        }
        ent=ent->next;
    }
}

int main()
{
    const char filename[] = "sample.json";
    json_object *jdata;
    struct lh_entry *entry;

    jdata = json_object_from_file(filename);
    if( jdata==NULL )
    {
        fprintf(stderr,"Unable to process %s\n",filename);
        exit(1);
    }

    entry=json_object_get_object(jdata)->head;
    parse_json_object(jdata,entry,0);

    return(0);
}    

Output

 'firstName' type is string, value: Simon
 'middleName' type is string, value: Bar
 'lastName' type is string, value: Sinister
 'address' type is JSON object:
    'street' type is string, value: 123 Evil Ave.
    'city' type is string, value: Bigtown
    'state' type is string, value: New York
    'zip' type is string, value: 12345
 'isCartoon' type is boolean, value: TRUE
 'IQ' type is Double
 'phones' type is array with 2 elements: 0 0
 'assistant' type is string, value: Cad Lackey
 'spouse' type is Null
 'favorite numbers' type is array with 4 elements: 2 13 23 66

Notes

* Several statements are wrapped in this code for better presentation in the book.