-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathnot_use_struct2json.c
More file actions
30 lines (26 loc) · 1.15 KB
/
not_use_struct2json.c
File metadata and controls
30 lines (26 loc) · 1.15 KB
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
cJSON *struct_to_json(void* struct_obj) {
Student *struct_student = (Student *) struct_obj;
cJSON *score, *score_element;
size_t index = 0;
/* 创建Student JSON对象 */
cJSON *json_student = cJSON_CreateObject();
/* 序列化数据到Student JSON对象 */
cJSON_AddNumberToObject(json_student, "id", struct_student->id);
cJSON_AddNumberToObject(json_student, "weight", struct_student->weight);
score = cJSON_CreateArray();
if (score) {
while (index < 8) {
score_element = cJSON_CreateNumber(struct_student->score[index++]);
cJSON_AddItemToArray(score, score_element);
}
cJSON_AddItemToObject(json_student, "score", score);
}
cJSON_AddStringToObject(json_student, "name", struct_student->name);
/* 序列化数据到Student.Hometown JSON对象 */
Hometown *hometown_struct = &(struct_student->hometown);
cJSON *hometown_json = cJSON_CreateObject();
cJSON_AddItemToObject(json_student, "hometown", hometown_json);
cJSON_AddStringToObject(hometown_json, "name", hometown_struct->name);
/* 返回Student JSON对象指针 */
return json_student;
}