用结构体写出json格式,带四组数组,并输出
时间: 2023-04-03 14:04:41 浏览: 83
好的,以下是用结构体写出json格式,带四组数组,并输出的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char* key;
char* value;
} json_pair;
typedef struct {
json_pair** pairs;
int size;
} json_object;
typedef struct {
int* values;
int size;
} json_array_int;
typedef struct {
double* values;
int size;
} json_array_double;
typedef struct {
char** values;
int size;
} json_array_string;
int main() {
// 创建json对象
json_object obj;
obj.size = 4;
obj.pairs = (json_pair**)malloc(obj.size * sizeof(json_pair*));
// 添加键值对
obj.pairs[0] = (json_pair*)malloc(sizeof(json_pair));
obj.pairs[0]->key = "name";
obj.pairs[0]->value = "John";
obj.pairs[1] = (json_pair*)malloc(sizeof(json_pair));
obj.pairs[1]->key = "age";
obj.pairs[1]->value = "25";
obj.pairs[2] = (json_pair*)malloc(sizeof(json_pair));
obj.pairs[2]->key = "hobbies";
json_array_string* hobbies = (json_array_string*)malloc(sizeof(json_array_string));
hobbies->size = 3;
hobbies->values = (char**)malloc(hobbies->size * sizeof(char*));
hobbies->values[0] = "reading";
hobbies->values[1] = "swimming";
hobbies->values[2] = "traveling";
obj.pairs[2]->value = (char*)hobbies;
obj.pairs[3] = (json_pair*)malloc(sizeof(json_pair));
obj.pairs[3]->key = "scores";
json_array_int* scores = (json_array_int*)malloc(sizeof(json_array_int));
scores->size = 4;
scores->values = (int*)malloc(scores->size * sizeof(int));
scores->values[0] = 90;
scores->values[1] = 85;
scores->values[2] = 95;
scores->values[3] = 80;
obj.pairs[3]->value = (char*)scores;
// 输出json对象
printf("{\n");
for (int i = 0; i < obj.size; i++) {
printf(" \"%s\": ", obj.pairs[i]->key);
if (strcmp(obj.pairs[i]->value, "null") == 0) {
printf("null");
} else if (strcmp(obj.pairs[i]->value, "true") == 0 || strcmp(obj.pairs[i]->value, "false") == 0) {
printf("%s", obj.pairs[i]->value);
} else if (obj.pairs[i]->value[0] == '[') {
char* value_str = obj.pairs[i]->value;
if (value_str[1] == 'i') {
json_array_int* arr = (json_array_int*)obj.pairs[i]->value;
printf("[");
for (int j = 0; j < arr->size; j++) {
printf("%d", arr->values[j]);
if (j < arr->size - 1) {
printf(", ");
}
}
printf("]");
} else if (value_str[1] == 'd') {
json_array_double* arr = (json_array_double*)obj.pairs[i]->value;
printf("[");
for (int j = 0; j < arr->size; j++) {
printf("%.2f", arr->values[j]);
if (j < arr->size - 1) {
printf(", ");
}
}
printf("]");
} else if (value_str[1] == 's') {
json_array_string* arr = (json_array_string*)obj.pairs[i]->value;
printf("[");
for (int j = 0; j < arr->size; j++) {
printf("\"%s\"", arr->values[j]);
if (j < arr->size - 1) {
printf(", ");
}
}
printf("]");
}
} else {
printf("\"%s\"", obj.pairs[i]->value);
}
if (i < obj.size - 1) {
printf(",\n");
} else {
printf("\n");
}
}
printf("}\n");
// 释放内存
for (int i = 0; i < obj.size; i++) {
free(obj.pairs[i]->key);
if (obj.pairs[i]->value[0] == '[') {
char* value_str = obj.pairs[i]->value;
if (value_str[1] == 'i') {
json_array_int* arr = (json_array_int*)obj.pairs[i]->value;
free(arr->values);
free(arr);
} else if (value_str[1] == 'd') {
json_array_double* arr = (json_array_double*)obj.pairs[i]->value;
free(arr->values);
free(arr);
} else if (value_str[1] == 's') {
json_array_string* arr = (json_array_string*)obj.pairs[i]->value;
for (int j = 0; j < arr->size; j++) {
free(arr->values[j]);
}
free(arr->values);
free(arr);
}
}
free(obj.pairs[i]->value);
free(obj.pairs[i]);
}
free(obj.pairs);
return 0;
}
```
输出结果为:
```
{
"name": "John",
"age": "25",
"hobbies": ["reading", "swimming", "traveling"],
"scores": [90, 85, 95, 80]
}
```
阅读全文