Hi.
You can use the json_object_foreach macro to iterate over all key-value pairs of an object:
Hope this help.
Petri
Alan Lewis <
alanl...@gmail.com> kirjoitti:
Hi,
I'm a new Jansson user, so forgive me if this question is stupid!
I am parsing a JSON formatted file, that contains an array of objects associated with a "key". I can locate the key, but I then want to iterate over the objects in the array that follow the key. Then I want to compare the (string) key value to see if it matches. If it does, I then need to get the values. Here's a sample of the file......
{ ...
REST OF THE FILE LEFT OUT FOR BREVITY. IT IS CORRECTLY FORMATTED
"domain" : [
{ "EN" : "
http://www.awebURL.com", "action" : [{"lostpassword" : "myaccount.aspx"}, {"support" : "support.aspx"}] },
{ "GR" : "
http://gr.somegreekURL.com" },
{ "ES" : "
http://www.someotherURL.es" }
],
"action" : [
{ "lostpassword" : "myaccount.aspx" },
{ "changepassword" : "myaccount.aspx" },
....]
}
I can find the object "domain", I then want to compare the object 'keys' within the array to find the one being sought. How can I check for the key string in the "domain" array objects?
Sample code..... (again, not all the file is here for brevity)
json_t *dom = json_object_get(root, "domain");
if ( json_is_array(dom) ) {
for ( int i = 0; i < json_array_size(dom); i++) {
country = json_array_get(dom, i); // HERE I AM TRYING TO RETRIEVE THE "key" STRING TO COMPARE??
if ( json_is_object(country)) {
if ( 0 = strcmp("EN", country_key_value() ) { // this is the logic I would like to implement!!
url = json_string_value(country);
fprintf(stdout, "We found country %s; ", url;
} // inner if
} // if
} // loop
} // outer if
I hope this makes some sense. I can't see how to achieve this from the Jansson documentation. The documentation seems to lean more on generating JSON than parsing.
Thanks for any help.
Alan
--
Jansson users mailing list
jansso...@googlegroups.com
http://groups.google.com/group/jansson-users