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

相关推荐
chuxinweihui12 分钟前
数据结构——二叉树,堆
c语言·开发语言·数据结构·学习·算法·链表
周而复始 否极泰来21 分钟前
深入浅出学会函数(上)
c语言·学习
陈大大陈29 分钟前
基于 C++ 的用户认证系统开发:从注册登录到Redis 缓存优化
java·linux·开发语言·数据结构·c++·算法·缓存
看到我,请让我去学习38 分钟前
C语言基础(day0424)
c语言·开发语言·数据结构
studyer_domi41 分钟前
Matlab 复合模糊PID
开发语言·matlab
猫猫头有亿点炸1 小时前
C语言斐波拉契数列2.0
c语言·开发语言·算法
刚入坑的新人编程1 小时前
C++多态
开发语言·c++
西柚小萌新1 小时前
【Python爬虫实战篇】--Selenium爬取Mysteel数据
开发语言·爬虫·python
努力写代码的熊大1 小时前
c语言中文件操作详解
c语言·开发语言
QUST-Learn3D1 小时前
高精度并行2D圆弧拟合(C++)
开发语言·c++