Search code examples
cjsonjson-lib

JsonParser in C with json-c Library | json_object_object_get(...) not declared


I am new here and try to implement a Json Parser. I find a Tutorial (https://linuxprograms.wordpress.com/2010/05/20/json-c-libjson-tutorial/) for a Json Parser Programm.

So i have a Json-File Input like this one:

{ "DefaultTest": "SS",
    "Send": { 
    "ip_hl": 4,
    "ip_v": 4,
    ....
    }
}

I use the Code in the Tutorial (https://linuxprograms.wordpress.com/2010/08/19/json_parser_json-c/) and try to understand this one. But i get an Error, that the Method json_object_object_get_get(jobj, key) is not declared.

So find a Wibesite (https://json-c.github.io/json-c/json-c-0.10/doc/html/deprecated.html) with Deprecated List in there for the json-lib. In this there is stay that i should use json_object_object_get_ex.

If I use the method i get this warning: assignment makes pointer from integer without a cast [-Wint-conversion] jobj = json_object_object_get_ex(jobj, key, NULL);

What is this mean? How can I fix it? My Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <json/json.h>

void json_parse(json_object * jobj) {
    enum json_type type;

    json_object_object_foreach(jobj, key, val)
    {
        printf("type: %d\n", type);
        type = json_object_get_type(val);

        switch (type) {
        case json_type_boolean:
        case json_type_double:
        case json_type_int:
        case json_type_string:
//          print_json_value(val);
            break;

        case json_type_object:
            printf("json_type_objectn");

//          jobj = json_object_object_get(jobj, key);
            jobj = json_object_object_get_ex(jobj, key, NULL);
            json_parse(jobj);
            break;

        case json_type_array:
//          printf("json_type_arrayn");
//          json_parse_array(jobj,key);
            break;

        }
    }
}

int main() {
    char *path = "../Paketspezifikationen/Default-File/DefaultConfig2.json";
    FILE *fp;
    char *data = NULL;

    if ((fp = fopen(path, "rb")) == NULL) {
    } else if (fseek(fp, 0, SEEK_END) != 0) {
        fclose(fp);
    } else {
        long size = ftell(fp);
        if (size > 0 && (data = (char *) malloc(size + 1)) != NULL) {
            fseek(fp, 0, SEEK_SET);
            if (fread(data, 1, size, fp) != (size_t) size) {
                free(data);
                data = NULL;
            } else {
                data[size] = '\0';
            }
        }
        fclose(fp);
    }
    printf("JSON string: %sn", data); // return data

    json_object * jobj = json_tokener_parse(data);

    json_parse(jobj);
}

In Main I read first the Json File and store it in a String and try to get all my keys and values from there. So i use the json parser.

Hope that you can help me.


Solution

  • The json_object_object_get_ex() function returns a json_bool (which is actually an int) indicating whether the given key is found in the parent JSON object, so you can't assign the function return value to jobj, which is a pointer: that's where the warning comes from.

    Since in your code you already have a pointer (in the val variable) to the JSON object corresponding to a given key, you can achieve what you want more simply by avoiding the call to json_object_object_get_ex() and calling json_parse(val).