• [置顶] cJSON库(构建json与解析json字符串)-c语言



     一、c语言获取json中的数据。

     

    1、先要有cJOSN库,两个文件分别是cJSON.c和cJSON.h。


    2、感性认识

     

    
    
    
    
    char * json = "{ "json" : { "id":1, "nodeId":11, "deviceId":111, "deviceName":"aaa", "ieee":"01212", "ep":"1111", "type":"bbb" }}";
    char * json1 = "{"id":1, "nodeId":11, "deviceId":111, "deviceName":"aaa"}";
    cJSON * root;
    cJSON * format;
    int value_int;
    char * value_string;
    
    root = cJSON_Parse(json); 
    format = cJSON_GetObjectItem(root,"json");   
    value_int = cJSON_GetObjectItem(format,"nodeId")->valueint; 
    value_string = cJSON_GetObjectItem(format,"ieee")->valuestring; 
    printf( "%d
    ", value_int );
    printf( "%s
    ", value_string );
    cJSON_Delete(root);
    	
    root = cJSON_Parse(json1); 
    value_int = cJSON_GetObjectItem(root,"id")->valueint; 
    value_string = cJSON_GetObjectItem(root,"deviceName")->valuestring; 
    printf( "%d
    ", value_int );
    printf( "%s
    ", value_string );
    cJSON_Delete(root);
    
    
    
    
    
    

    结果:

     

    11
    01212
    1
    aaa
    

    二、cJSON库


    1、json的数据结构 


    c语言中json数据是采用链表存储的 

     

    typedef struct cJSON {   

     

        struct cJSON *next,*prev;// 数组 对象数据中用到   

     

        struct cJSON *child;// 数组 和对象中指向子数组对象或值   

     

        int type;// 元素的类型,如是对象还是数组   

     

        char *valuestring;// 如果是字符串   

     

        int valueint; // 如果是数值   

     

        double valuedouble;// 如果类型是cJSON_Number   

     

        char *string;// The item's name string, if this item is the child of, or is in the list of subitems of an object.   

     

    } cJSON; 

     


    三、cJSON使用

     

    {   

     

        "name": "Jack ("Bee") Nimble",    

     

        "format": {   

     

            "type":       "rect",    

     

            "width":      1920,    

     

            "height":     1080,    

     

            "interlace":  false,    

     

            "frame rate": 24   

     

        }   

     

     

        "name": "Jack ("Bee") Nimble", 

     

        "format": {

     

            "type":       "rect", 

     

            "width":      1920, 

     

            "height":     1080, 

     

            "interlace":  false, 

     

            "frame rate": 24

     

        }

     

    }


     

    1、字符串解析成json结构体

     

    1):讲字符串解析成json结构体。 

     

    cJSON *root = cJSON_Parse(my_json_string); 


     

    2):获取某个元素  

     

    cJSON *format = cJSON_GetObjectItem(root,"format");   

     

    int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint; 

     

    int framerate = cJSON_GetObjectItem(format,"frame rate")->valueint;


     

    3):讲json结构体转换成字符串 

     

    char *rendered=cJSON_Print(root); 


     

    4):删除 

     

    cJSON_Delete(root); 


     

    2:构建一个json结构体  

     

    cJSON *root,*fmt;   

     

    root=cJSON_CreateObject();     

     

    cJSON_AddItemToObject(root, "name", cJSON_CreateString("Jack ("Bee") Nimble"));   

     

    cJSON_AddItemToObject(root, "format", fmt=cJSON_CreateObject());   

     

    cJSON_AddStringToObject(fmt,"type",     "rect");   

     

    cJSON_AddNumberToObject(fmt,"width",        1920);   

     

    cJSON_AddNumberToObject(fmt,"height",       1080);   

     

    cJSON_AddFalseToObject (fmt,"interlace");   

     

    cJSON_AddNumberToObject(fmt,"frame rate",   24)

    out =cJSON_Print(root);

    printf("%s ",out); 

    cJSON_Delete(root);

    free(out);







     

  • 相关阅读:
    ubuntu全版本通用换源教程
    【Pybind11】Python调用C++接口(Ubuntu下编译OpenCV)
    ubuntu18.04如何安装python3.5及其pip安装
    smb和rdp暴破差异分析
    如何修改smb服务的默认445端口?——官方回答是无法修改,但是可以使用端口转发
    开集识别——流形
    10种常见的进程注入技术的总结
    webmine和cryptowebminer挖矿——一句<iframe width=0 height=0 frameborder=0 src='https://webmine.cz/worker?key=[YOUR_API_KEY]'></iframe>搞定
    进程隐藏与进程保护(SSDT Hook 实现)(一)
    deepMiner —— 本质上类似coinhive,也是后端开启nodejs连接矿池,默认的连接门罗币矿池
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3246882.html
Copyright © 2020-2023  润新知