導航:首頁 > 編程語言 > c提交json格式數據

c提交json格式數據

發布時間:2024-11-14 22:15:43

『壹』 怎麼用C語言獲取jsON中的數據

用C語言獲取JSON中的數據的方法是使用 CJSON。

以下簡單介紹用CJSON的思路及實現:

1)創建json,從json中獲取數據。

#nclude <stdio.h>

#include "cJSON.h"

char * makeJson()

{

cJSON * pJsonRoot = NULL;


pJsonRoot = cJSON_CreateObject();

if(NULL == pJsonRoot)

{

//error happend here

return NULL;

}

cJSON_AddStringToObject(pJsonRoot, "hello", "hello world");

cJSON_AddNumberToObject(pJsonRoot, "number", 10010);

cJSON_AddBoolToObject(pJsonRoot, "bool", 1);

cJSON * pSubJson = NULL;

pSubJson = cJSON_CreateObject();

if(NULL == pSubJson)

{

// create object faild, exit

cJSON_Delete(pJsonRoot);

return NULL;

}

cJSON_AddStringToObject(pSubJson, "subjsonobj", "a sub json string");

cJSON_AddItemToObject(pJsonRoot, "subobj", pSubJson);

char * p = cJSON_Print(pJsonRoot);

// else use :

// char * p = cJSON_PrintUnformatted(pJsonRoot);

if(NULL == p)

{

//convert json list to string faild, exit

//because sub json pSubJson han been add to pJsonRoot, so just delete pJsonRoot, if you also delete pSubJson, it will coremp, and error is : double free

cJSON_Delete(pJsonRoot);

return NULL;

}

//free(p);

cJSON_Delete(pJsonRoot);

return p;

}

void parseJson(char * pMsg)

{

if(NULL == pMsg)

{

return;

}

cJSON * pJson = cJSON_Parse(pMsg);

if(NULL == pJson)

{

// parse faild, return

return ;

}

// get string from json

cJSON * pSub = cJSON_GetObjectItem(pJson, "hello");

if(NULL == pSub)

{

//get object named "hello" faild

}

printf("obj_1 : %s ", pSub->valuestring);

// get number from json

pSub = cJSON_GetObjectItem(pJson, "number");

if(NULL == pSub)

{

//get number from json faild

}

printf("obj_2 : %d ", pSub->valueint);

// get bool from json

pSub = cJSON_GetObjectItem(pJson, "bool");

if(NULL == pSub)

{

// get bool from json faild

}

printf("obj_3 : %d ", pSub->valueint);

// get sub object

pSub = cJSON_GetObjectItem(pJson, "subobj");

if(NULL == pSub)

{

// get sub object faild

}

cJSON * pSubSub = cJSON_GetObjectItem(pSub, "subjsonobj");

if(NULL == pSubSub)

{

// get object from subject object faild

}

printf("sub_obj_1 : %s ", pSubSub->valuestring);

cJSON_Delete(pJson);

}


int main()

{

char * p = makeJson();

if(NULL == p)

{

return 0;

}

printf("%s ", p);

parseJson(p);

free(p);//這里不要忘記釋放內存,cJSON_Print()函數或者cJSON_PrintUnformatted()產生的內存,使用free(char *)進行釋放

return 0;

}

2)創建json數組和解析json數組

//創建數組,數組值是另一個JSON的item,這里使用數字作為演示

char * makeArray(int iSize)

{

cJSON * root = cJSON_CreateArray();

if(NULL == root)

{

printf("create json array faild ");

return NULL;

}

int i = 0;


for(i = 0; i < iSize; i++)

{

cJSON_AddNumberToObject(root, "hehe", i);

}

char * out = cJSON_Print(root);

cJSON_Delete(root);


return out;

}

//解析剛剛的CJSON數組

void parseArray(char * pJson)

{

if(NULL == pJson)

{

return ;

}

cJSON * root = NULL;

if((root = cJSON_Parse(pJson)) == NULL)

{

return ;

}

int iSize = cJSON_GetArraySize(root);

for(int iCnt = 0; iCnt < iSize; iCnt++)

{

cJSON * pSub = cJSON_GetArrayItem(root, iCnt);

if(NULL == pSub)

{

continue;

}

int iValue = pSub->valueint;

printf("value[%2d] : [%d] ", iCnt, iValue);

}

cJSON_Delete(root);

return;

}

閱讀全文

與c提交json格式數據相關的資料

熱點內容
怎麼把ps文件變成源文件 瀏覽:807
如何把圖片放進這個文件夾 瀏覽:530
誰有qq認證圖標復制 瀏覽:881
2014年web伺服器測試工具 瀏覽:192
win7鏡像文件怎麼用 瀏覽:983
多數據can怎麼發送 瀏覽:997
什麼是夏威夷果種植技術app 瀏覽:160
上海學習編程哪個地方好 瀏覽:958
股票軟體與交易編程軟體哪個好 瀏覽:216
linux如何查看一個文件的位置 瀏覽:911
c頭文件無法打開源文件stdafx 瀏覽:750
蘋果x的桌面上可以建文件夾 瀏覽:368
ug8532位破解版安裝教程 瀏覽:490
電腦網路如何重新驅動 瀏覽:125
win10連接伺服器命令 瀏覽:228
盧克raid活動20升級卷 瀏覽:969
機票預訂什麼網站好 瀏覽:690
讀寫xml配置文件 瀏覽:153
java爬蟲解析html 瀏覽:861
約瑟夫集合java 瀏覽:275

友情鏈接