简单使用cJSON进行json格式的转换

今天看了json格式,也了解了使用C语言进行json格式的转换,下面写了一个简单的测试记录一下

Ubuntu 下使用cJSON进行格式转换,首先需要下载安装cJson进行编译安装:

github 地址: https://github.com/DaveGamble/cJSON
下载
git clone https://github.com/DaveGamble/cJSON.git
参考 Readme 进行编译

下载后的文件里面有两个主要的文件,cJSON.c和cJSON.h,使用的时候把这两个文件包含在源文件目录下即可:
在这里插入图片描述

下面是我简单的测试:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cJSON.h"

//实现json格式和普通格式的相互转换

//评论的结构体
typedef struct COMMENT{
    int user_id;
    const  char *nick_name;
    const  char *comment;
}COMMENT;


//打印 json格式
void printJson(cJSON *json){
    if(NULL == json){
        return;
    }
     printf("JSON格式内容:\n");
    //
    char *cjson = cJSON_Print(json);  //将cJSON数据解析成JSON字符串,并在堆中开辟一块内存来存储new malloc
    printf("json : %s\n",cjson);      //使用完毕要free或者delete
}

//打印结构体形式
void printComment(COMMENT *comment){
    if(NULL == comment){
        return;
    }

    printf("结构体内容:\n");
    printf("user_id: %d\n",comment->user_id);
    printf("nick_name: %s\n",comment->nick_name);
    printf("comment: %s\n",comment->comment);
    ptnitf("\n\n");

}



//释放
void freeComment(COMMENT *comment){
    if(comment != NULL){
        free(comment);
        comment = NULL;
    }else{
        return ; 
    }
}

void freeCjson(cJSON *json){
    if(json != NULL){
        free(json);
        json = NULL;
    }else{
        return ; 
    }
}

//comment --> json
cJSON* convertToJson(COMMENT *comment){
    if(NULL == comment){
        return NULL;
    }
    //
    cJSON *json = cJSON_CreateObject();  //malloc  
    //调用cJSON_AddItenToObject 和 cJSON_CreateNumber
    cJSON_AddNumberToObject(json,"user_id",comment->user_id);
    //cJSON_AddItemToObject(json,"nick_name",cJSON_CreateString(comment->nick_name));
    //调用cJSON_AddItenToObject 和 cJSON_CreateString
    cJSON_AddStringToObject(json,"nick_name",comment->nick_name);
    cJSON_AddStringToObject(json,"comment",comment->comment);

    return json;
}

COMMENT *convertToComment(cJSON * json){
    if(NULL == json){
        return NULL;
    }

    //使用key获取相应的value
    cJSON *json_user_id = cJSON_GetObjectItem(json,"user_id");
    cJSON *json_nick_name = cJSON_GetObjectItem(json,"nick_name");
    cJSON *Json_comment = cJSON_GetObjectItem(json,"comment");


    COMMENT *comment = (COMMENT *)malloc(sizeof(COMMENT));
    comment->user_id = 0;
    comment->nick_name = NULL;
    comment->comment = NULL;

    //valueint 当类型为cJSON_Number时,value的值
    comment->user_id = json_user_id->valueint;
    comment->nick_name = json_nick_name->valuestring;
    comment->comment = Json_comment->valuestring;

    return comment;
}

//创建评论
COMMENT * createComment(){
    COMMENT *comment = (COMMENT *)malloc(sizeof(COMMENT));
    comment->user_id = 1;
    comment->nick_name = "zzzA";
    comment->comment = "孤独是难以避免的";
    return comment;
}


void test(){
    COMMENT * comment = createComment();
    printComment(comment);

    cJSON *json =  convertToJson(comment); 
    printJson(json);

    COMMENT * comment2 = convertToComment(json);
    printComment(comment2);

    freeComment(comment);
    freeComment(comment2);
    freeCjson(json);
}

int main(int argc,char **argv){
    test();
    return 0;
}




编译:gcc cJSON.c cJson_test.c -o test -lm
运行:./test
结果:
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值