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);
        //cJSON_Delete(reqItem);
        //cJSON_Delete(Request);
    }

    //释放
    //cJSON_Delete(arrItem);
    cJSON_Delete(Message);
    //打印
    displayNode(head);
    
    return 0;
}
View Code

 

以下是报文模板:

{
    "FileVersion" : "1.0.0.0",
    "Message":[
        {
            "MSGTYPE": "112211",
            "ReqTagLen": "2",
            "ResTagLen": "2",
            "Request": [
                { "code": "RESPCODE", "data":"1", "type": "string" },
                { "code": "RESPINFO", "data":"2", "type": "string" }
            ],
            "Response": [
                { "code": "RESPCODE", "data":"testcode1", "type": "string" },
                { "code": "RESPINFO", "data":"testinfo1", "type": "string" }
            ]
        },
        {
            "MSGTYPE": "112222",
            "ReqTagLen": "2",
            "ResTagLen": "2",
            "Request": [
                { "code": "RESPCODE", "data":"", "type": "string" },
                { "code": "RESPINFO", "data":"","type": "string" }
            ],
            "Response": [
                { "code": "RESPCODE", "data":"testcode2", "type": "string" },
                { "code": "RESPINFO", "data":"testinfo2", "type": "string" }
            ]        
        }
    ]
}
View Code
posted @ 2019-12-03 14:11  蓦然而然  阅读(795)  评论(0编辑  收藏  举报