Reputation: 38
I'm parsing a json file with cJSON. Everything is going well. But one of the data elements is a Boolean, and the problem is I don't understand how to use cJSON's functions for reading Boolean types. The data file contains lines like:
"isDaytime": true,
I attempted to use a code block like:
if (cJSON_IsTrue("isDaytime")==0) {
printf("isDaytime = TRUE\n");
} else {
printf("isDaytime = FALSE\n");
}
When I try to compile this with gcc, I get the following message:
undefined reference to `cJSON_IsTrue'
I think perhaps cJSON represents it as an int? I don't know. At any rate, does anyone know how I should be calling cJSON_IsTrue() ? Or perhaps I shouldn't be calling cJSON_IsTrue() at all. If not, how should I be checking this type of data element? If it was an int, I'd be done. If it was a char *, I'd be done. I'm just not sure how I'm supposed to test whether it's true or false.
EDIT: According to the documentation for the cJSON library, here:https://github.com/DaveGamble/cJSON
Because the entire library is only one C file and one header file, you can just copy cJSON.h and cJSON.c to your projects source and start using it.
So this was how I was using cJSON. I had just included the two files in my project, and everything was working like a charm. Until I tried using the cJSON_IsTrue() function. I wasn't able to see how to correctly use the function from the documentation, or from the source files. I figured I'd just try to use it, "wrongly", and then correct myself via whatever error messages came up.
I didn't expect to receive the "undefined reference" error message. I looked through the source, and indeed the function was there...
David Collins suggested I needed to link to the cjson library when compiling. I was including the source, so I didn't think that was truly the answer. But okay. So I followed the directions and actually installed the library on my system. And I got the same undefined reference error message.
It turned out that the location where the library installed itself: /usr/local/lib, wasn't being referenced. I copied the installed files to the correct directory for my system and the cJSON_IsTrue() function is now available when I compile with the -lcjson flag.
So how to use it? This is how I used it:
`if ( cJSON_IsTrue(cJSON_GetObjectItem(subitem, "isDayTime")) == 1) {
/* do something if it's true */
} else {
/* do something if it's false */
}`
This seems to have done the trick. Thanks again @David Collins!
Upvotes: 1
Views: 7594
Reputation: 3022
cJSON_IsTrue()
doesn't expect a string literal; it expects a cJSON object.
Suppose json_string
is a char *
representation of your full JSON object. Then you could try the following.
First of all parse the top-level JSON object.
cJSON *json = CJSON_Parse(json_string);
if (json == NULL) {
// Handle error and abort if appropriate
}
Then extract your child object.
cJSON *daytime_json = cJSON_GetObjectItemCaseSensitive(json, "isDayTime");
if (cJSON_IsTrue(daytime_json) == 0) {
// Whatever you need to do next ...
}
// Etc.
Finally, don't forget to de-allocate memory / clean up afterwards.
cJSON_Delete(json);
You mentioned that
When I try to compile this with gcc, I get the following message:
undefined reference tocJSON_IsTrue
You need to link the cjson
library when compiling. E.g., if using the gcc
compiler, you would use something like
gcc -o json-test json-test.c -lcjson
Upvotes: 6