zoukankan      html  css  js  c++  java
  • cJSON使用

    c语言源码下载地址:https://github.com/DaveGamble/cJSON

    其他语言源码下载地址:http://www.json.org/

    用 cJSON_PrintUnformatted(root) 或者 cJSON_Print(root);来将json对象转换成普通的字符串,并且都是以该json对象的根为基点。两个API的区别即是:一个是没有格式的:也就是转换出的字符串中间不会有" " " "之类的东西存在,而cJSON_Print(root);打印出来是人看起来很舒服的格式。仅此而已。

     

     

    cJSON的使用

    JSON格式的生产 

    以下代码不需要执行cJSON_Delete(sub_js),因为删除父CJSON对象时,会删除子CJSON对象

        cJSON *root=NULL;
        cJSON *sub_js=NULL;
        char *out=NULL;
        
        root = cJSON_CreateObject();
        cJSON_AddStringToObject(root, "ver", "v1.0.0");
        cJSON_AddStringToObject(root, "did", "066EFF3638324D4D43183319");
        cJSON_AddStringToObject(root, "hardware_ver", "V1.0.0");
        cJSON_AddStringToObject(root, "software_ver", "V1.0.0");
        cJSON_AddItemToObject(root, "data", sub_js = cJSON_CreateObject());
        cJSON_AddNumberToObject(sub_js, "status", 1);
        cJSON_AddTrueToObject(sub_js, "material");
        cJSON_AddTrueToObject(sub_js, "power_on");
        cJSON_AddNumberToObject(sub_js, "qty", 123);
        cJSON_AddStringToObject(root, "dt", "2017-11-04T05:15:52");
        
        out=cJSON_Print(root);
        cJSON_Delete(root);    
        debug("%s
    ",out);    
        free(out);

    打印出

    {
        "ver":    "v1.0.0",
        "did":    "066EFF3638324D4D43183319",
        "hardware_ver":    "V1.0.0",
        "software_ver":    "V1.0.0",
        "data":    {
            "status":    1,
            "material":    true,
            "power_on":    true,
            "qty":    123
        },
        "dt":    "2017-11-04T05:15:52"
    }

    解析JSON格式(读取JSON格式数据,只要是在同一个{}内,不需要从上往下按顺序读,支持乱序)

    {
        "wifi":    {
            "ssid":    "hello World",
            "pwd":    "234sdlfkj23"
        },
        "mqtt":    {
            "server":    "192.168.31.240",
            "port":    1883,
            "clientid":    "asdfghjklzxcvbnm",
            "username":    "1234567asdfghj",
            "password":    "asdfghjkerty"
        },
        "option":    {
            "working_signal_last_time":    15,
            "material_empty_signal_last_time":    6,
            "material_full_signal_last_time":    3,
            "status_send_time":    30
        }
    }

     UART_RxBuf_tmp就是上面的JSON格式数据

                char *out;
                cJSON *json, *item1, *item2;
                
                json=cJSON_Parse((char const *)UART_RxBuf_tmp);
                if (!json) 
                {
                    debug("Error before:%s
    ",cJSON_GetErrorPtr());
                }
                else
                {
                    item1 = cJSON_GetObjectItem(json, "wifi");
                    item2 = cJSON_GetObjectItem(item1, "ssid");
                    debug("%s: %s", item2->string, item2->valuestring);
                    item2 = cJSON_GetObjectItem(item1, "pwd");
                    debug("%s: %s", item2->string, item2->valuestring);
                    debug("%s",wifi_information);
                    item1 = cJSON_GetObjectItem(json, "mqtt");
                    item2 = cJSON_GetObjectItem(item1, "server");
                    debug("%s: %s", item2->string, item2->valuestring);
                    item2 = cJSON_GetObjectItem(item1, "port");
                    debug("%s: %d", item2->string, item2->valueint);
                    debug("%s",server_information);
                    item2 = cJSON_GetObjectItem(item1, "clientid");
                    debug("%s: %s", item2->string, item2->valuestring);
                    item2 = cJSON_GetObjectItem(item1, "username");
                    debug("%s: %s", item2->string, item2->valuestring);
                    item2 = cJSON_GetObjectItem(item1, "password");
                    debug("%s: %s", item2->string, item2->valuestring);
                    item1 = cJSON_GetObjectItem(json, "option");
                    item2 = cJSON_GetObjectItem(item1, "working_signal_last_time");
                    debug("%s: %d", item2->string, item2->valueint);
                    item2 = cJSON_GetObjectItem(item1, "material_empty_signal_last_time");
                    debug("%s: %d", item2->string, item2->valueint);
                    item2 = cJSON_GetObjectItem(item1, "material_full_signal_last_time");
                    debug("%s: %d", item2->string, item2->valueint);
                    item2 = cJSON_GetObjectItem(item1, "status_send_time");
                    debug("%s: %d", item2->string, item2->valueint);
                    
                    out=cJSON_Print(json);
                    cJSON_Delete(json);
                    debug("%s
    ",out);
                    free(out);
                }

    创建带数组的JSON

        cJSON *root=NULL;
        cJSON *sub_js=NULL;
        char *out=NULL;
        
        root = cJSON_CreateObject();
        cJSON_AddStringToObject(root, "ver", protocol_ver);
        cJSON_AddStringToObject(root, "did", (char const*)g_MCU_uniqueID_str);
        cJSON_AddStringToObject(root, "hardware_ver", hardware_ver);
        cJSON_AddStringToObject(root, "software_ver", software_ver);
        cJSON_AddItemToObject(root, "data", sub_js = cJSON_CreateObject());
        
        cJSON *array;
        array = cJSON_CreateArray();
        cJSON_AddItemToObject(sub_js, "M", array);
        cJSON_AddItemToArray(array, cJSON_CreateBool(1));
        cJSON_AddItemToArray(array, cJSON_CreateBool(1));
        cJSON_AddItemToArray(array, cJSON_CreateBool(1));
        cJSON_AddItemToArray(array, cJSON_CreateBool(0));
        cJSON_AddItemToArray(array, cJSON_CreateBool(0));
        array = cJSON_CreateArray();
        cJSON_AddItemToObject(sub_js, "D", array);
        cJSON_AddItemToArray(array, cJSON_CreateNumber(12312));
        cJSON_AddItemToArray(array, cJSON_CreateNumber(12334));
        cJSON_AddItemToArray(array, cJSON_CreateNumber(967425));
        
        char time[20];
        get_formatted_time(time);
        cJSON_AddStringToObject(root, "dt", (char const*)time);
        
        out=cJSON_Print(root);
        cJSON_Delete(root);
        debug("%s
    ",out);
        MQTT_publish(out, "init");
        vPortFree(out);
  • 相关阅读:
    C语言ll作业01
    C语言寒假大作战04
    C语言寒假大作战03
    C语言寒假大作战02
    C语言寒假大作战01
    C语言I作业12—学期总结
    C语言I博客作业11
    C语言I博客作业10
    第三章预习笔记-运算方法和运算部件
    非数值数据的编码表示
  • 原文地址:https://www.cnblogs.com/god-of-death/p/9248772.html
Copyright © 2011-2022 走看看