C语言将json内容替换成另外一种数据格式代码
时间: 2023-12-24 17:04:57 浏览: 25
以下是一个示例代码,将输入的 JSON 字符串转换成 YAML 格式,并将结果输出到控制台:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <yaml.h>
#include <cJSON.h>
void print_yaml(yaml_document_t *doc, yaml_node_t *node, int level);
void print_scalar(yaml_document_t *doc, yaml_node_t *node) {
printf("%s", yaml_scalar_node_get_value(node));
}
void print_sequence(yaml_document_t *doc, yaml_node_t *node, int level) {
yaml_node_item_t *item;
for (item = node->data.sequence.items.start; item < node->data.sequence.items.top; item++) {
print_yaml(doc, yaml_document_get_node(doc, *item), level + 1);
}
}
void print_mapping(yaml_document_t *doc, yaml_node_t *node, int level) {
yaml_node_pair_t *pair;
for (pair = node->data.mapping.pairs.start; pair < node->data.mapping.pairs.top; pair++) {
yaml_node_t *key = yaml_document_get_node(doc, pair->key);
yaml_node_t *value = yaml_document_get_node(doc, pair->value);
for (int i = 0; i < level; i++) {
printf(" ");
}
printf("%s: ", yaml_scalar_node_get_value(key));
print_yaml(doc, value, level + 1);
}
}
void print_yaml(yaml_document_t *doc, yaml_node_t *node, int level) {
switch (node->type) {
case YAML_SCALAR_NODE:
print_scalar(doc, node);
printf("\n");
break;
case YAML_SEQUENCE_NODE:
print_sequence(doc, node, level);
break;
case YAML_MAPPING_NODE:
print_mapping(doc, node, level);
break;
default:
break;
}
}
int main(int argc, char *argv[]) {
char *json_str = "{\"name\": \"Alice\", \"age\": 20, \"friends\": [\"Bob\", \"Charlie\"]}";
cJSON *root = cJSON_Parse(json_str);
yaml_emitter_t emitter;
yaml_document_t document;
yaml_node_t *node = NULL;
if (!yaml_emitter_initialize(&emitter)) {
printf("Failed to initialize emitter.\n");
return 1;
}
if (!yaml_document_initialize(&document, NULL, NULL, NULL, 1, 1)) {
printf("Failed to initialize document.\n");
return 1;
}
node = yaml_document_add_mapping(&document, NULL, YAML_BLOCK_MAPPING_STYLE);
cJSON_ArrayForEach(root, item) {
char *key_str = item->string;
cJSON *value = item->child;
yaml_node_t *key_node = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)key_str, strlen(key_str), YAML_PLAIN_SCALAR_STYLE);
yaml_node_t *value_node = NULL;
switch (value->type) {
case cJSON_String:
value_node = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)value->valuestring, strlen(value->valuestring), YAML_DOUBLE_QUOTED_SCALAR_STYLE);
break;
case cJSON_Number:
value_node = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)cJSON_Print(value), strlen(cJSON_Print(value)), YAML_PLAIN_SCALAR_STYLE);
break;
case cJSON_Array:
value_node = yaml_document_add_sequence(&document, NULL, YAML_BLOCK_SEQUENCE_STYLE);
cJSON_ArrayForEach(value, item) {
yaml_node_t *item_node = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)value->valuestring, strlen(value->valuestring), YAML_DOUBLE_QUOTED_SCALAR_STYLE);
yaml_sequence_append(value_node, item_node);
}
break;
default:
break;
}
yaml_mapping_append(node, key_node, value_node);
}
yaml_emitter_set_output_file(&emitter, stdout);
yaml_emitter_dump(&emitter, &document);
yaml_emitter_delete(&emitter);
yaml_document_delete(&document);
cJSON_Delete(root);
return 0;
}
```
该示例代码使用了 `cJSON` 库来解析 JSON 字符串,并使用 `libyaml` 库来生成 YAML 格式的文档。在 `print_yaml` 函数中,根据节点类型分别调用 `print_scalar`、`print_sequence` 和 `print_mapping` 函数来递归打印节点内容。在 `main` 函数中,先将 JSON 字符串解析成 cJSON 对象,然后根据 cJSON 对象生成相应的 YAML 节点,并最终使用 `yaml_emitter_dump` 函数将 YAML 文档输出到控制台。