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

相关推荐
xueyinan7 分钟前
小刚说C语言刷题—1088求两个数M和N的最大公约数
c语言
ClearViper39 分钟前
Java的多线程笔记
java·开发语言·笔记
敷啊敷衍9 分钟前
深入探索 C++ 中的 string 类:从基础到实践
开发语言·数据结构·c++
学地理的小胖砸19 分钟前
【Python 面向对象】
开发语言·python
神经毒素1 小时前
WEB安全--Java安全--LazyMap_CC1利用链
java·开发语言·网络·安全·web安全
酷炫码神1 小时前
C#语法基础
开发语言·c#
ddd...e_bug1 小时前
GMT之Bash语言使用
开发语言·bash
码农秋1 小时前
填坑记: 古董项目Apache POI 依赖异常排除
开发语言·tomcat·jsp·poi·依赖冲突
qq_653644461 小时前
如何查看打开的 git bash 窗口是否是管理员权限打开
开发语言·windows·git·bash
sadoshi1 小时前
phpstudy的Apache添加AddType application/x-httpd-php .php .php5配置无效的处理方式
开发语言·php·apache