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

相关推荐
IT毕设实战小研几秒前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
cui__OaO1 小时前
Linux软件编程--线程
linux·开发语言·线程·互斥锁·死锁·信号量·嵌入式学习
鱼鱼说测试2 小时前
Jenkins+Python自动化持续集成详细教程
开发语言·servlet·php
艾莉丝努力练剑2 小时前
【洛谷刷题】用C语言和C++做一些入门题,练习洛谷IDE模式:分支机构(一)
c语言·开发语言·数据结构·c++·学习·算法
CHEN5_023 小时前
【Java基础面试题】Java基础概念
java·开发语言
Cx330❀4 小时前
【数据结构初阶】--排序(五):计数排序,排序算法复杂度对比和稳定性分析
c语言·数据结构·经验分享·笔记·算法·排序算法
杜子不疼.4 小时前
《Python学习之字典(一):基础操作与核心用法》
开发语言·python·学习
落霞的思绪4 小时前
Java设计模式详细解读
java·开发语言·设计模式
阿巴~阿巴~4 小时前
深入解析C++ STL链表(List)模拟实现
开发语言·c++·链表·stl·list
..过云雨5 小时前
01.【数据结构-C语言】数据结构概念&算法效率(时间复杂度和空间复杂度)
c语言·数据结构·笔记·学习