cJSON的使用记录

最近用到c语言对json的解析,用来处理收发报文。

做一个使用记录。

c语言有第三方的封装接口,就是cJSON,将对应的.h文件加入到自己的代码中,编译时加上.c文件编译。

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h> #include "cJSON.h" #define MOSTDOMAIN 200 struct dataVolume{ char code[40]; char data[4096]; char type[10]; }; struct msg{ char msgType[20]; int reqLen; int resLen; dataVolume request[MOSTDOMAIN]; dataVolume response[MOSTDOMAIN]; struct msg *next; }; struct msg *appendNode(struct msg *head); void displayNode(struct msg *head); struct msg *appendNode(struct msg *head, struct msg *node) { struct msg *pr = head; if(node == NULL){ printf("node is NULL!"); return head; } if(head == NULL){ head = node; }else{ while(pr->next != NULL){//is NULL,it's last node pr = pr->next; } pr->next = node; } node->next = NULL; return head; } void displayNode(msg *head){ struct msg *p = head; int j = 1; while(p != NULL){ printf("mstype: [%s]\n",p->msgType); for(int i = 0; i < p->reqLen; i++){ printf("request %d: [%s.%s.%s]\n", i, p->request[i].code, p->request[i].data, p->request[i].type); } for(int i = 0; i < p->resLen; i++){ printf("response %d: [%s.%s.%s]\n", i, p->response[i].code, p->response[i].data, p->response[i].type); } p = p->next; j++; } } int main(int argc, char *argv[]) { struct msg *head = NULL;//创建头指针 //head = (struct msg *)malloc(sizeof(struct msg));  FILE *f; long lenth; char *content; cJSON *json; f = fopen("./MessageTable.json","r"); fseek(f, 0, SEEK_END); lenth = ftell(f); fseek(f, 0, SEEK_SET); content=(char*)malloc(lenth+1); fread(content, 1, lenth, f); fclose(f); json = cJSON_Parse(content); if(!json){ printf("Error before: [%s]\n",cJSON_GetErrorPtr()); } //获取数组对象集合 cJSON *Message = cJSON_GetObjectItem(json, "Message"); int mesLen = cJSON_GetArraySize(Message); printf("mesLen: [%d]\n",mesLen); cJSON *arrItem; for(int i = 0; i < mesLen; i++){ struct msg *node = NULL; node = (struct msg *)malloc(sizeof(struct msg)); //获取每一个数组对象的子对象 arrItem = cJSON_GetArrayItem(Message, i); char *mstype = cJSON_GetObjectItem(arrItem, "MSGTYPE")->valuestring; strcpy(node->msgType, mstype); //printf("mstype: [%s]\n",mstype); //子对象为数组的对象的解析 cJSON *Request = cJSON_GetObjectItem(arrItem, "Request"); int reqLen = cJSON_GetArraySize(Request); node->reqLen = reqLen; cJSON *reqItem; for(int j = 0; j < reqLen; j++){ reqItem = cJSON_GetArrayItem(Request, j); char *code = cJSON_GetObjectItem(reqItem, "code")->valuestring; char *data = cJSON_GetObjectItem(reqItem, "data")->valuestring; char *type = cJSON_GetObjectItem(reqItem, "type")->valuestring; struct dataVolume tag; strcpy(tag.code, code); strcpy(tag.data, data); strcpy(tag.type, type); node->request[j] = tag; //printf("reqItem %d: [%s.%s.%s]\n", j, code, data, type);  } cJSON *Response = cJSON_GetObjectItem(arrItem, "Response"); int ResLen = cJSON_GetArraySize(Response); node->resLen = ResLen; cJSON *resItem; for(int k = 0; k < ResLen; k++){ resItem = cJSON_GetArrayItem(Response, k); char *code = cJSON_GetObjectItem(resItem, "code")->valuestring; char *data = cJSON_GetObjectItem(resItem, "data")->valuestring; char *type = cJSON_GetObjectItem(resItem, "type")->valuestring; struct dataVolume tag; strcpy(tag.code, code); strcpy(tag.data, data); strcpy(tag.type, type); node->response[k] = tag; //printf("resItem %d: [%s.%s.%s]\n", k, code, data, type);  } //加入到链表中 head = appendNode(head, node); //释放 //cJSON_Delete(resItem); //cJSON_Delete(Response); 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值