C語(yǔ)言提供了JSON解析庫(kù),可以方便地從JSON中取出數(shù)據(jù)類型。
#include#include #include #include int main(int argc, char **argv) { const char *json_str = "{\"name\":\"John\",\"age\":30,\"isMarried\":true}"; struct json_object *obj = json_tokener_parse(json_str); enum json_type type; // name json_object_object_get_ex(obj, "name", &tmp); type = json_object_get_type(tmp); printf("name type: %s\n", json_type_to_name(type)); // age json_object_object_get_ex(obj, "age", &tmp); type = json_object_get_type(tmp); printf("age type: %s\n", json_type_to_name(type)); // isMarried json_object_object_get_ex(obj, "isMarried", &tmp); type = json_object_get_type(tmp); printf("isMarried type: %s\n", json_type_to_name(type)); json_object_put(tmp); json_object_put(obj); return 0; }
以上代碼解析了一個(gè)JSON字符串,然后從中取出了三個(gè)鍵值對(duì)的數(shù)據(jù)類型,分別為"name"的字符串類型、"age"的整型類型和"isMarried"的布爾類型。