當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
json格式与cJSON函数库
生活随笔
收集整理的這篇文章主要介紹了
json格式与cJSON函数库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
json的格式
json數組
- char array[23] = " safasdfsdaf";
- 中括號[整型,字符串,布爾類型,json數組,json對象],數據類型可以不一樣
json對象
- {}中是一些鍵值對
- 例如:
- key值:必須是字符串,不重復,key值是搜索唯一的標識
- value值:json對象,json數組,布爾,整型,字符串
json數組+json對象
{ "name1": "zhang3" , "name2": "li4" ,"張三" : { "別名" : "老王","性別" : "男" ,"年齡" : 34,"孩子" : ["小紅","小綠","小黑"]} }cJson結構體
/* 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
創建一個json對象
cJson * cJson_CreateObject(void);往json對象中添加數據成員
void cJson_AddltemToObject(cJson *object, //json對象cosnt char *string, //key值cJson *item //value值(int,string,array,obj) );從緩沖區中解析出JSON結構
extern cJSON *cJSON_Parse(const char *value);解析一塊JSON數據返回cJSON結構,在使用完之后調用cJSON_Delete函數釋 放json對象結構
將傳入的JSON結構轉化為字符串
extern char *cJSON_Print(cJSON *item);(可用于輸出到輸出設備),使用完之后free(char *);
- 返回值需要free釋放
- FILE * fp=fopen();
- fwrite();
- fclose();
將JSON結構所占用的數據空間釋放
void cJSON_Delete(cJSON *c)創建一個值類型的數據
extern cJSON *cJSON_CreateNumber(double num); extern cJSON *cJSON_CreateString(const char *string); extern cJSON *cJSON_CreateArray(void);創建一個對象(文檔)
extern cJSON *cJSON_CreateObject(void);數組創建以及添加
cJSON *cJSON_CreateIntArray(const int *numbers,int count); void cJSON_AddItemToArray(cJSON *array, cJSON *item);json嵌套
向對象中增加鍵值對
cJSON_AddItemToObject(root, "rows", 值類型數據相關函數());向對象中增加數組
cJSON_AddItemToObject(root, "rows", cJSON_CreateArray());向數組中增加對象
cJSON_AddItemToArray(rows, cJSON_CreateObject());解析json文件
將字符串解析為JSON結構
cJSON* cJSON_Parse(cosnt char * value); //返回值需要使用cJSON_Delete釋放根據鍵值查找json結點
cJSON * cJSON_GetObjectltem(cJSON* object, //當前json對象const char * string //key值 );獲取json數組中元素的個數
int cJSON_GetArraySize(cJSON* array);根據數組下標找到對應的數組元素
cJSON* cJSON_GetArrayltem(cJSON* attay,int index);判斷是否有可以值對應的鍵值對
int cJSON_HasObjectltem(cJSON* object,const char* string);c語言調用cJSON函數庫創建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[]) {//創建對象cJSON* obj = cJSON_CreateObject();//創建子對象cJSON* subObj = cJSON_CreateObject();//向子對象中添加鍵值對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));//創建json數組cJSON* array = cJSON_CreateArray();//array數組中添加元素cJSON_AddItemToArray(array,cJSON_CreateNumber(123));cJSON_AddItemToArray(array,cJSON_CreateBool(1));cJSON_AddItemToArray(array,cJSON_CreateString("hellow world"));//數組中的對象cJSON* subsub = cJSON_CreateObject();cJSON_AddItemToObject(subsub,"梅賽德斯奔馳",cJSON_CreateString("心所向,持以恒"));cJSON_AddItemToArray(array,subsub);cJSON_AddItemToObject(subObj,"other",array);//obj中添加key - valuecJSON_AddItemToObject(obj,"奔馳",subObj);//數據格式化char* data = cJSON_Print(obj);FILE* fp = fopen("car.json","w");fwrite(data,sizeof(char),strlen(data)+1,fp);fclose(fp);return 0;}總結
以上是生活随笔為你收集整理的json格式与cJSON函数库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue怎么引入jq??? 财富值2
- 下一篇: 试管婴儿受孕成功后会腹痛吗