Last active
December 18, 2018 02:15
-
-
Save allanjos/a1b5dec590b56dcfed4dad311e0f7ded to your computer and use it in GitHub Desktop.
json-c parse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <json-c/json.h> | |
| #include <stdio.h> | |
| const char *json_get_type_name(enum json_type type); | |
| void json_parse_object(json_object *jobj); | |
| void json_parse_array(json_object *jobj, char *key); | |
| void json_print_value(json_object *jobj); | |
| const char *json_get_type_name(enum json_type type) | |
| { | |
| switch (type) { | |
| case json_type_null: | |
| return "json_type_null"; | |
| break; | |
| case json_type_boolean: | |
| return "json_type_boolean"; | |
| break; | |
| case json_type_double: | |
| return "json_type_double"; | |
| break; | |
| case json_type_int: | |
| return "json_type_int"; | |
| break; | |
| case json_type_object: | |
| return "json_type_object"; | |
| break; | |
| case json_type_array: | |
| return "json_type_array"; | |
| break; | |
| case json_type_string: | |
| return "json_type_string"; | |
| break; | |
| } | |
| return ""; | |
| } | |
| void json_print_value(json_object *jobj) | |
| { | |
| enum json_type type = json_object_get_type(jobj); | |
| switch (type) { | |
| case json_type_boolean: | |
| printf(" value (boolean): %s\n", json_object_get_boolean(jobj) ? "true": "false"); | |
| break; | |
| case json_type_double: | |
| printf(" value (double): %lf\n", json_object_get_double(jobj)); | |
| break; | |
| case json_type_int: | |
| printf(" value (int): %d\n", json_object_get_int(jobj)); | |
| break; | |
| case json_type_string: | |
| printf(" value (string): %s\n", json_object_get_string(jobj)); | |
| break; | |
| case json_type_null: | |
| case json_type_object: | |
| case json_type_array: | |
| printf(" json type not handled\n"); | |
| break; | |
| } | |
| } | |
| void json_parse_array(json_object *jobj, char *key) | |
| { | |
| void json_parse_object(json_object *jobj); | |
| json_object *jarray = jobj; | |
| if (key) { | |
| jarray = json_object_object_get(jobj, key); | |
| } | |
| int array_length = json_object_array_length(jarray); | |
| json_object *jarray_item; | |
| enum json_type type; | |
| for (int i = 0; i < array_length; i++) { | |
| jarray_item = json_object_array_get_idx(jarray, i); | |
| type = json_object_get_type(jarray_item); | |
| switch (type) { | |
| case json_type_array: | |
| json_parse_array(jarray_item, NULL); | |
| break; | |
| case json_type_object: | |
| json_parse_object(jarray_item); | |
| break; | |
| default: | |
| printf(" array item %d: ",i); | |
| json_print_value(jarray_item); | |
| } | |
| } | |
| } | |
| void json_parse_object(json_object *jobj) | |
| { | |
| enum json_type type; | |
| json_object_object_foreach(jobj, key, value) { | |
| printf("KEY: %s\n", key); | |
| type = json_object_get_type(value); | |
| printf(" type: %s\n", json_get_type_name(type)); | |
| switch (type) { | |
| case json_type_boolean: | |
| case json_type_double: | |
| case json_type_int: | |
| case json_type_string: | |
| json_print_value(value); | |
| break; | |
| case json_type_object: | |
| jobj = json_object_object_get(jobj, key); | |
| json_parse_object(jobj); | |
| break; | |
| case json_type_array: | |
| json_parse_array(jobj, key); | |
| break; | |
| case json_type_null: | |
| break; | |
| } | |
| } | |
| } | |
| int main() | |
| { | |
| char *string = "{" | |
| "\"glossary\": {" | |
| "\"title\": \"example glossary\"," | |
| "\"GlossDiv\": {" | |
| "\"title\": \"S\"," | |
| "\"GlossList\": {" | |
| "\"GlossEntry\": {" | |
| "\"ID\": \"SGML\"," | |
| "\"SortAs\": \"SGML\"," | |
| "\"GlossTerm\": \"Standard Generalized Markup Language\"," | |
| "\"Acronym\": \"SGML\"," | |
| "\"Abbrev\": \"ISO 8879:1986\"," | |
| "\"GlossDef\": {" | |
| "\"para\": \"A meta-markup language, used to create markup languages such as DocBook.\"," | |
| "\"GlossSeeAlso\": [\"GML\", \"XML\"]" | |
| "}," | |
| "\"GlossSee\": \"markup\"" | |
| "}" | |
| "}" | |
| "}" | |
| "}" | |
| "}"; | |
| printf("JSON string: %s\n", string); | |
| json_object *jobj = json_tokener_parse(string); | |
| json_parse_object(jobj); | |
| printf("\n"); | |
| } | |
| // Compile under FreeBSD: | |
| // cc -I /usr/local/include jsonc-parse-test.c -o jsonc-parse-test -L /usr/local/lib -ljson-c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment