日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

json格式与cJSON函数库

發(fā)布時(shí)間:2023/11/30 javascript 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 json格式与cJSON函数库 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

json的格式

  • 鍵/值對(duì) key:value,用半角冒號(hào)分割
  • 文檔對(duì)象 JSON對(duì)象寫(xiě)在花括號(hào)中,可以包含多個(gè)鍵/值對(duì)。
  • 數(shù)組 JSON 數(shù)組在方括號(hào)中書(shū)寫(xiě): 數(shù)組成員可以是對(duì)象,值,也可以是數(shù)組(只要有意義)。 { "stars":[ { "name":"Faye" ,"address":"北京" }, { "name":"andy" ,"address":"香港" }, { "name":"eddie","address":"臺(tái)灣" }, ] }
  • json數(shù)組

    • char array[23] = " safasdfsdaf";
    • 中括號(hào)[整型,字符串,布爾類(lèi)型,json數(shù)組,json對(duì)象],數(shù)據(jù)類(lèi)型可以不一樣

    json對(duì)象

    • {}中是一些鍵值對(duì)
    • 例如:
    { "name1": "zhang3" , "name2": "li4" }
    • key值:必須是字符串,不重復(fù),key值是搜索唯一的標(biāo)識(shí)
    • value值:json對(duì)象,json數(shù)組,布爾,整型,字符串

    json數(shù)組+json對(duì)象

    { "name1": "zhang3" , "name2": "li4" ,"張三" : { "別名" : "老王","性別" : "男" ,"年齡" : 34,"孩子" : ["小紅","小綠","小黑"]} }

    cJson結(jié)構(gòu)體

    /* cJSON Types: */ #define cJSON_Invalid (0) #define cJSON_False (1 << 0) #define cJSON_True (1 << 1) #define cJSON_NULL (1 << 2) #define cJSON_Number (1 << 3) #define cJSON_String (1 << 4) #define cJSON_Array (1 << 5) #define cJSON_Object (1 << 6) #define cJSON_Raw (1 << 7) /* raw json */#define cJSON_IsReference 256 #define cJSON_StringIsConst 512/* The cJSON structure: */ typedef struct cJSON {/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */struct cJSON *next;struct cJSON *prev;/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */struct cJSON *child;/* The type of the item, as above. */int type;/* The item's string, if type==cJSON_String and type == cJSON_Raw */char *valuestring;/* The item's number, if type==cJSON_Number */int valueint;/* The item's number, if type==cJSON_Number */double valuedouble;/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */char *string; } cJSON;typedef struct cJSON_Hooks {void *(*malloc_fn)(size_t sz);void (*free_fn)(void *ptr); } cJSON_Hooks;

    cjson

    創(chuàng)建一個(gè)json對(duì)象

    cJson * cJson_CreateObject(void);

    往json對(duì)象中添加數(shù)據(jù)成員

    void cJson_AddltemToObject(cJson *object, //json對(duì)象cosnt char *string, //key值cJson *item //value值(int,string,array,obj) );

    從緩沖區(qū)中解析出JSON結(jié)構(gòu)

    extern cJSON *cJSON_Parse(const char *value);

    解析一塊JSON數(shù)據(jù)返回cJSON結(jié)構(gòu),在使用完之后調(diào)用cJSON_Delete函數(shù)釋 放json對(duì)象結(jié)構(gòu)

    將傳入的JSON結(jié)構(gòu)轉(zhuǎn)化為字符串

    extern char *cJSON_Print(cJSON *item);

    (可用于輸出到輸出設(shè)備),使用完之后free(char *);

    • 返回值需要free釋放
    • FILE * fp=fopen();
    • fwrite();
    • fclose();

    將JSON結(jié)構(gòu)所占用的數(shù)據(jù)空間釋放

    void cJSON_Delete(cJSON *c)

    創(chuàng)建一個(gè)值類(lèi)型的數(shù)據(jù)

    extern cJSON *cJSON_CreateNumber(double num); extern cJSON *cJSON_CreateString(const char *string); extern cJSON *cJSON_CreateArray(void);

    創(chuàng)建一個(gè)對(duì)象(文檔)

    extern cJSON *cJSON_CreateObject(void);

    數(shù)組創(chuàng)建以及添加

    cJSON *cJSON_CreateIntArray(const int *numbers,int count); void cJSON_AddItemToArray(cJSON *array, cJSON *item);

    json嵌套

    向?qū)ο笾性黾渔I值對(duì)

    cJSON_AddItemToObject(root, "rows", 值類(lèi)型數(shù)據(jù)相關(guān)函數(shù)());

    向?qū)ο笾性黾訑?shù)組

    cJSON_AddItemToObject(root, "rows", cJSON_CreateArray());

    向數(shù)組中增加對(duì)象

    cJSON_AddItemToArray(rows, cJSON_CreateObject());

    解析json文件

    將字符串解析為JSON結(jié)構(gòu)

    cJSON* cJSON_Parse(cosnt char * value); //返回值需要使用cJSON_Delete釋放

    根據(jù)鍵值查找json結(jié)點(diǎn)

    cJSON * cJSON_GetObjectltem(cJSON* object, //當(dāng)前json對(duì)象const char * string //key值 );

    獲取json數(shù)組中元素的個(gè)數(shù)

    int cJSON_GetArraySize(cJSON* array);

    根據(jù)數(shù)組下標(biāo)找到對(duì)應(yīng)的數(shù)組元素

    cJSON* cJSON_GetArrayltem(cJSON* attay,int index);

    判斷是否有可以值對(duì)應(yīng)的鍵值對(duì)

    int cJSON_HasObjectltem(cJSON* object,const char* string);

    c語(yǔ)言調(diào)用cJSON函數(shù)庫(kù)創(chuàng)建json文件

    示例

    #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/types.h> #include<sys/stat.h> #include<string.h> #include"cJSON.h" int main(int argc,const char * argv[]) {//創(chuàng)建對(duì)象cJSON* obj = cJSON_CreateObject();//創(chuàng)建子對(duì)象cJSON* subObj = cJSON_CreateObject();//向子對(duì)象中添加鍵值對(duì)cJSON_AddItemToObject(subObj,"factory",cJSON_CreateString("一汽大眾"));cJSON_AddItemToObject(subObj,"last",cJSON_CreateNumber(31));cJSON_AddItemToObject(subObj,"price",cJSON_CreateNumber(83));cJSON_AddItemToObject(subObj,"sell",cJSON_CreateNumber(49));cJSON_AddItemToObject(subObj,"sum",cJSON_CreateNumber(80));//創(chuàng)建json數(shù)組cJSON* array = cJSON_CreateArray();//array數(shù)組中添加元素cJSON_AddItemToArray(array,cJSON_CreateNumber(123));cJSON_AddItemToArray(array,cJSON_CreateBool(1));cJSON_AddItemToArray(array,cJSON_CreateString("hellow world"));//數(shù)組中的對(duì)象cJSON* subsub = cJSON_CreateObject();cJSON_AddItemToObject(subsub,"梅賽德斯奔馳",cJSON_CreateString("心所向,持以恒"));cJSON_AddItemToArray(array,subsub);cJSON_AddItemToObject(subObj,"other",array);//obj中添加key - valuecJSON_AddItemToObject(obj,"奔馳",subObj);//數(shù)據(jù)格式化char* data = cJSON_Print(obj);FILE* fp = fopen("car.json","w");fwrite(data,sizeof(char),strlen(data)+1,fp);fclose(fp);return 0;}

    總結(jié)

    以上是生活随笔為你收集整理的json格式与cJSON函数库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。