jewfro
jewfro

Reputation: 253

How to extract value of json object key, value pairs using json-c

I'm trying to use this function, as the only example I've been able to find is a deprecated version of this function, which has two parameters. The newer function has three, but when I try to run compile, I get the error: dereferencing pointer to incomplete type deprecated function : *jobj = json_object_object_get(jobj,key) new function : *jobj = json_object_object_get_ex(jobj,key,value)

I've just tried the following : json_object_get_string(json_object_object_get(new_obj, "foo"))

But I'm getting the error message that this is deprecated. If I used the newer function, I need to know 'value'. But that's the point, I only know the key and I want to extract the value. Any help would be appreciated as I haven't been able to find any examples other than the above

Upvotes: 4

Views: 15942

Answers (1)

erichamion
erichamion

Reputation: 4527

I'm not sure if you still need this, but the documentation matches the error you're getting. The json_object_object_get() function is deprecated in favor of json_object_object_get_ex().

Look more closely at the function parameters:

json_bool json_object_object_get_ex (struct json_object *obj, const char *key,
struct json_object **value)

The value parameter is for output, not input. You supply the address of (or a pointer to) a json_object*, and the function fills it in with the return value. For example:

json_object* f(json_object* rootObj, const char* key)
{
    json_object* returnObj;
    if (json_object_object_get_ex(rootObj, key, &returnObj)
    {
        return returnObj;
    }
    return NULL;
}

Source: json-c API Documentation

Upvotes: 6

Related Questions