C语言:cJSON将struct结构体与JSON互相转换

文章目录

    • [struct 转 json](#struct 转 json)
    • [json 转 struct](#json 转 struct)

文档: https://github.com/DaveGamble/cJSON

项目结构

bash 复制代码
.
├── libs
│   ├── cJSON.c
│   └── cJSON.h
└── main.c

示例

struct 转 json

c 复制代码
#include "libs/cJSON.h"
#include <stdio.h>

// define data struct
typedef struct Student
{
    int age;
    char *name;
} Student;

int main(int argc, char const *argv[])
{
    // 1. create struct
    Student student;
    student.age = 18;
    student.name = "Tom";

    // 2. struct to json object
    cJSON *item = cJSON_CreateObject();
    if (cJSON_AddStringToObject(item, "name", student.name) == NULL)
    {
        goto end;
    }

    if (cJSON_AddNumberToObject(item, "age", student.age) == NULL)
    {
        goto end;
    }

    // 3. print json string
    char *json = cJSON_Print(item);
    printf("%s\n", json);

    // 4. free json
end:
    cJSON_Delete(item);

    return 0;
}

输出

bash 复制代码
$ gcc main.c libs/cJSON.c && ./a.out

{
        "name": "Tom",
        "age":  18
}

json 转 struct

c 复制代码
#include "libs/cJSON.h"
#include <stdio.h>
#include <memory.h>

// define data struct
typedef struct Student
{
    int age;
    char *name;
} Student;

char *Student_to_string(char *result, Student *student)
{
    if (student == NULL)
    {
        return result;
    }

    sprintf(result,
            "Student {\n name: \"%s\",\n age: %d\n}\n",
            student->name,
            student->age);

    return result;
}

int main(int argc, char const *argv[])
{
    Student student;
    cJSON *item;
    cJSON *name_item;
    cJSON *age_item;
    char result[100];

    char *json = "{\"name\": \"Tom\",\"age\":  18}";

    // 1. parse json
    item = cJSON_Parse(json);
    if (item == NULL)
    {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL)
        {
            fprintf(stderr, "Error before: %s\n", error_ptr);
        }
        goto end;
    }

    // 2. convert to struct
    name_item = cJSON_GetObjectItem(item, "name");
    if(cJSON_IsString(name_item) && name_item->valuestring != NULL){
        student.name = name_item->valuestring;
    }

    age_item = cJSON_GetObjectItem(item, "age");
    if(cJSON_IsNumber(age_item)){
        student.age = age_item->valueint;
    }

    Student_to_string(result, &student);
    printf("%s", result);

end:
    // 3. free json
    cJSON_Delete(item);

    return 0;
}

输出

bash 复制代码
$ gcc main.c libs/cJSON.c && ./a.out

Student {
 name: "Tom",
 age: 18

参考

cJSON库学习------C语言结构体与JSON互相转换

https://www.cnblogs.com/BearMan0047/p/16063422.html

相关推荐
黑客思维者1 天前
《我是如何用C语言写工控系统的漏洞和Bug》连载(1)内容大纲
c语言·bug·工控漏洞
中国胖子风清扬1 天前
Rust 序列化技术全解析:从基础到实战
开发语言·c++·spring boot·vscode·后端·中间件·rust
我就是全世界1 天前
【存储选型终极指南】RustFS vs MinIO:5大维度深度对决,95%技术团队的选择秘密!
开发语言·分布式·rust·存储
j_xxx404_1 天前
数据结构:栈和队列力扣算法题
c语言·数据结构·算法·leetcode·链表
南莺莺1 天前
假设一个算术表达式中包含圆括号、方括号和花括号3种类型的括号,编写一个算法来判别,表达式中的括号是否配对,以字符“\0“作为算术表达式的结束符
c语言·数据结构·算法·
yudiandian20141 天前
【QT 5.12.12 打包-Windows 平台下】
开发语言·qt
要记得喝水1 天前
C#某公司面试题(含题目和解析)--1
开发语言·windows·面试·c#·.net
金融数据出海1 天前
黄金金融期货数据API对接技术文档
开发语言·金融·github