1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| #include <stdio.h> #include <string.h> #include <json-c/json.h> #include <json-c/json_tokener.h> #include <json-c/json_object.h> #include <json-c/json_inttypes.h> #include <json-c/json_util.h>
const char *json_get_string_value_by_field(struct json_object *json, const char *p_field) { struct json_object *string_json = NULL;
json_object_object_get_ex(json, p_field, &string_json); if (NULL == string_json) { printf("json_object_object_get error %s", p_field); return NULL; }
if (json_type_string == json_object_get_type(string_json)) { return json_object_get_string(string_json); }
return NULL; }
int json_get_int_value_by_field(struct json_object *json, const char *p_field) { struct json_object *int_json = NULL;
json_object_object_get_ex(json, p_field, &int_json); if (NULL == int_json) { printf("json_object_object_get error %s", p_field); return -1; }
if (json_type_int == json_object_get_type(int_json)) { return (int)json_object_get_int(int_json); }
return -1; }
const char *json_get_string_value(struct json_object *json) { if (json_type_string == json_object_get_type(json)) { return json_object_get_string(json); }
return NULL; }
struct json_object *json_get_json_object_by_field(struct json_object *json, const char *p_field) { struct json_object *json_obj = NULL;
json_object_object_get_ex(json, p_field, &json_obj); if (NULL == json_obj) { printf("json_object_object_get error %s", p_field); return NULL; }
return json_obj; }
int json_is_array(struct json_object *json) { if (json_type_array == json_object_get_type(json)) { return 0; }
return -1; }
void printFunc(struct json_object *Carl) { if (Carl == NULL) { return; }
const char *name; int age; struct json_object *PhoneArray = NULL; name = json_get_string_value_by_field(Carl, "name"); age = json_get_int_value_by_field(Carl, "age"); PhoneArray = json_get_json_object_by_field(Carl, "phone");
printf("name:%s\nage:%d\n", name, age); printf("phone num:\n");
if (0 == json_is_array(PhoneArray)) { for (int i = 0; i < json_object_array_length(PhoneArray); i++) { printf("%s\n", json_object_get_string(json_object_array_get_idx(PhoneArray, i))); } } }
int main() { struct json_object *Carl = NULL; struct json_object *Sam = NULL; struct json_object *PhoneObject = NULL; char *root_json = "{\"name\":\"Carl\",\"age\":24,\"phone\":[\"12345678901\",\"12345678902\"]}";
Carl = json_tokener_parse(root_json);
printFunc(Carl);
Sam = json_object_new_object(); if (Sam == NULL) { return -1; }
json_object_object_add(Sam, "name", json_object_new_string("Sam")); json_object_object_add(Sam, "age", json_object_new_int(21));
PhoneObject = json_object_new_array(); if (PhoneObject == NULL) { json_object_put(Sam); return -1; } json_object_array_add(PhoneObject, json_object_new_string("12345678903")); json_object_array_add(PhoneObject, json_object_new_string("12345678904")); json_object_object_add(Sam, "phone", PhoneObject);
printFunc(Sam);
json_object_put(Carl); json_object_put(Sam); return 0; }
|