在C語言中,我們經常需要處理JSON格式的數據。而在處理JSON數據時,我們往往需要對其進行篩選。下面是一些關于C JSON篩選的基礎技巧。
// 示例JSON數據 { "name": "張三", "age": 18, "gender": "男", "score": { "math": 90, "english": 80, "chinese": 70 }, "hobbies": ["籃球", "游戲", "音樂"] } // 篩選基本數據類型 json_object *name = json_object_object_get(json_data, "name"); char *name_str = json_object_get_string(name); printf("姓名:%s\n", name_str); int age = json_object_get_int(json_object_object_get(json_data, "age")); printf("年齡:%d\n", age); json_object *gender = json_object_object_get(json_data, "gender"); char *gender_str = json_object_get_string(gender); printf("性別:%s\n", gender_str); // 篩選JSON對象類型 json_object *score = json_object_object_get(json_data, "score"); int math_score = json_object_get_int(json_object_object_get(score, "math")); printf("數學成績:%d\n", math_score); // 篩選JSON數組類型 json_object *hobbies = json_object_object_get(json_data, "hobbies"); int hobbies_len = json_object_array_length(hobbies); printf("愛好:"); for (int i = 0; i< hobbies_len; i++) { json_object *hobby = json_object_array_get_idx(hobbies, i); char *hobby_str = json_object_get_string(hobby); printf("%s", hobby_str); if (i != hobbies_len - 1) { printf(","); } } printf("\n");
上一篇c json服務
下一篇c json結構字符串