c語言中json函數(shù) c++ json字符串

怎么用C語言獲取JSON中的數(shù)據(jù)?

用C語言獲取JSON中的數(shù)據(jù)的方法是使用 CJSON。

目前成都創(chuàng)新互聯(lián)公司已為上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、成都網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計、惠安網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

以下簡單介紹用CJSON的思路及實現(xiàn):

1)創(chuàng)建json,從json中獲取數(shù)據(jù)。

#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 coredump, 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\n", pSub-valuestring);

// get number from json

pSub = cJSON_GetObjectItem(pJson, "number");

if(NULL == pSub)

{

//get number from json faild

}

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

// get bool from json

pSub = cJSON_GetObjectItem(pJson, "bool");

if(NULL == pSub)

{

// get bool from json faild

}

printf("obj_3 : %d\n", 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\n", pSubSub-valuestring);

cJSON_Delete(pJson);

}

int main()

{

char * p = makeJson();

if(NULL == p)

{

return 0;

}

printf("%s\n", p);

parseJson(p);

free(p);//這里不要忘記釋放內(nèi)存,cJSON_Print()函數(shù)或者cJSON_PrintUnformatted()產(chǎn)生的內(nèi)存,使用free(char *)進(jìn)行釋放

return 0;

}

2)創(chuàng)建json數(shù)組和解析json數(shù)組

//創(chuàng)建數(shù)組,數(shù)組值是另一個JSON的item,這里使用數(shù)字作為演示

char * makeArray(int iSize)

{

cJSON * root = cJSON_CreateArray();

if(NULL == root)

{

printf("create json array faild\n");

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數(shù)組

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]\n", iCnt, iValue);

}

cJSON_Delete(root);

return;

}

有兩種方法:

一是標(biāo)準(zhǔn)的輸出輸入方式 比如新建一個磁盤文件c:\a.txt, 將鍵盤輸入的一字符串寫到文件中:

FILE *ft;

char str[50];

ft=fopen("c:\\a.txt","w+");

printf("輸入一個字符串:");

scanf("%s",str);

fputs(str,ft);

fclose(ft);

//重新打開這個文件并讀出字符串,顯示在屏幕上 ft=fopen("c:\\a.txt","rt");

fgets(str,50,ft);

fclose(ft); printf("%s",str);

二是低級輸入輸出方式 仍如上例:

int hd; char str[50]; printf("輸入一個字符串:");

scanf("%s",str);

hd=open("c:\\a.txt",O_CREAT|O_TEXT|O_WRONLY);

write(hd,str,strlen(str));

close(hd); //重新打開這個文件并讀出字符串,顯示在屏幕上。

hd=open("c:\\a.txt",O_TEXT|O_RDONLY); read(hd,str,50);

close(hd); printf("%s",str)。

JSON解析器json-c

JSON-C實現(xiàn)了一個引用計數(shù)對象模型,它允許您輕松地使用C語言來構(gòu)建JSON對象,將它們輸出為JSON格式的字符串,并將JSON格式字符串解析回JSON對象的C語言表示形式。它的目標(biāo)是符合 RFC 7159 標(biāo)準(zhǔn)。

使用automake的編譯過程如下:

使用cmake編譯的過程如下:

cmake可選的幾個編譯選項為:

要使用json-c,最簡單的方式是包含json.h頭文件即可,或者最好是下列更具體的頭文件之一:

詳細(xì)且全面的API介紹文檔:

JSON-C支持的JSON對象類型有7種:

下面系列函數(shù)用于創(chuàng)建一個JSON對象:

給JSON對象增加字段(不會增加引用計數(shù)):

刪除json對象的指定字段,被刪除的對象引用計數(shù)減去1,如果這個val沒有更多的所有者,這個key對應(yīng)的val被free,否則這個val的引用保存在內(nèi)存中:

增加一個元素到j(luò)son數(shù)組的末尾,obj引用計數(shù)不會增加,增加字段的方式更加緊湊;如果需要獲取val的引用,需要用json_object_get()來傳遞該對象:

替換json數(shù)組中的值:

json數(shù)組的排序,這里需要自己寫排序函數(shù):

獲取json對象的長度,依據(jù)字段的數(shù)目:

獲取json對象的哈希表:

獲取對象的數(shù)組列表:

獲取json的類型:

獲取json數(shù)組對象的長度:

獲取json對象的bool值,int和double對象是0轉(zhuǎn)換為FALSE,否則返回TRUE;非0長度的字符串返回TRUE;其他對象非空的話,返回TRUE:

獲取json對象的長度,如果參數(shù)不是string類型的json,返回0:

按照索引獲取json數(shù)組的對象:

轉(zhuǎn)換json對象到c字符串格式:

獲取JSON中指定類型的數(shù)值:

將字符串轉(zhuǎn)換為json對象:

以下兩個函數(shù)配合使用,前者獲取該對象指針的所有權(quán),引用計數(shù)加1,如果對象已經(jīng)被釋放,返回NULL;后者引用計數(shù)減1,如果對象已經(jīng)被釋放,返回1:

類型判斷:

json_util.h提供了有關(guān)文件讀寫操作的函數(shù),這個文件的內(nèi)容是json格式的:

json是什么

JSON(JavaScript?Object Notation, JS 對象簡譜) 是一種輕量級的數(shù)據(jù)交換格式。它基于?ECMAScript?(歐洲計算機(jī)協(xié)會制定的js規(guī)范)的一個子集,采用完全獨立于編程語言的文本格式來存儲和表示數(shù)據(jù)。

簡潔和清晰的層次結(jié)構(gòu)使得 JSON 成為理想的數(shù)據(jù)交換語言。 易于人閱讀和編寫,同時也易于機(jī)器解析和生成,并有效地提升網(wǎng)絡(luò)傳輸效率。簡單來說:json就是一種在各個編程語言中流通的數(shù)據(jù)格式,負(fù)責(zé)不同編程語言中的數(shù)據(jù)傳遞和交互。

擴(kuò)展資料

注意事項:

1,json的鍵值對的鍵部分,必須用雙引號"包裹,單引號都不行(所以如果在鍵中出現(xiàn)了關(guān)鍵字,也被字符化了),而js中對象沒有強(qiáng)制要求(所以在鍵中不允許出現(xiàn)關(guān)鍵字)。

2,json的鍵值對的值部分,不允許出現(xiàn)函數(shù)function,undefined,NaN,但是可以有null,js中對象的值中可以出現(xiàn)。

3,json數(shù)據(jù)結(jié)束后,不允許出現(xiàn)沒有意義的逗號,如:{"name":"admin","age":18,},注意看數(shù)據(jù)結(jié)尾部分18的后面的逗號,不允許出現(xiàn)。

參考資料來源:百度百科-Json

c語言 解析json字符串

你好,你用json-c庫,編譯通過了嗎?我是在ubuntu里使用json-c庫,但是無法編譯通過,報錯 undefined reference to 'json_tokener_parse',類似的函數(shù)沒定義的錯誤,你是怎么調(diào)用的json-c庫?請教一下,謝謝!

分享名稱:c語言中json函數(shù) c++ json字符串
本文URL:http://muchs.cn/article34/hgigse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、做網(wǎng)站軟件開發(fā)、響應(yīng)式網(wǎng)站網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)