josn常用函數(shù)c語(yǔ)言 join函數(shù)c語(yǔ)言

JSON解析器json-c

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

創(chuàng)新互聯(lián)建站專注于網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站開發(fā)。公司秉持“客戶至上,用心服務(wù)”的宗旨,從客戶的利益和觀點(diǎn)出發(fā),讓客戶在網(wǎng)絡(luò)營(yíng)銷中找到自己的駐足之地。尊重和關(guān)懷每一位客戶,用嚴(yán)謹(jǐn)?shù)膽B(tài)度對(duì)待客戶,用專業(yè)的服務(wù)創(chuàng)造價(jià)值,成為客戶值得信賴的朋友,為客戶解除后顧之憂。

使用automake的編譯過程如下:

使用cmake編譯的過程如下:

cmake可選的幾個(gè)編譯選項(xiàng)為:

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

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

JSON-C支持的JSON對(duì)象類型有7種:

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

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

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

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

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

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

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

獲取json對(duì)象的哈希表:

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

獲取json的類型:

獲取json數(shù)組對(duì)象的長(zhǎng)度:

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

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

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

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

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

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

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

類型判斷:

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

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

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

以下簡(jiǎn)單介紹用CJSON的思路及實(shí)現(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ù)組值是另一個(gè)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)的輸出輸入方式 比如新建一個(gè)磁盤文件c:\a.txt, 將鍵盤輸入的一字符串寫到文件中:

FILE *ft;

char str[50];

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

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

scanf("%s",str);

fputs(str,ft);

fclose(ft);

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

fgets(str,50,ft);

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

二是低級(jí)輸入輸出方式 仍如上例:

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

scanf("%s",str);

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

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

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

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

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

C#json有哪些常用類庫(kù)

據(jù)結(jié)構(gòu)應(yīng)該是對(duì)應(yīng)的,這里的例子可能忽略了部分屬性和數(shù)據(jù)類型,僅供參考:

//Json數(shù)據(jù)的對(duì)象結(jié)構(gòu)

public class MyJson

{

public ListMyTreeNodeBean beanList

}

//樹形結(jié)構(gòu)中的對(duì)象結(jié)構(gòu)

public class MyBean

{

public string id

public string title

public string note

public MyImage image

public string maxid

public string fold

public string putright

public ListMyTreeNodeBean children

public MyBean()

{

}

}

//對(duì)象里面Image的結(jié)構(gòu)

public class MyImage

{

public string url

public string border

public string height

public string width

}

2.因?yàn)閖son是樹形結(jié)構(gòu)的數(shù)據(jù),所以需要遞歸遍歷去尋找對(duì)應(yīng)ID的對(duì)象

//這個(gè)類負(fù)責(zé)按照ID尋找對(duì)象

public class MyHelper

{

//FindById函數(shù)的重載函數(shù),用來第一次調(diào)用

public static MyBean FindById(string id, string jsonString)

{

return FindById(id, jsonString, null);

}

//FindById遞歸尋找函數(shù)

public static MyBean FindById(string id, string jsonString, ListMyBean beanList)

{

if (beanList == null) {

//第一次調(diào)用的時(shí)候,尋找的列表是最高層的根底下的對(duì)象列表

//將json數(shù)據(jù)轉(zhuǎn)換成C#對(duì)應(yīng)類型(用了Newtonsoft.Json)

MyJson json = JavaScriptConvert.DeserializeObjectMyJson(jsonString);

beanList = json.beanList;

}

//遍歷對(duì)象列表,尋找對(duì)應(yīng)ID的對(duì)象

MyBean returnBean = null;

foreach (MyBean bean in beanList) {

if (bean.id == id) {

//找到了

returnBean = bean;

} else {

//遞歸尋找:

//如果不是,就尋找此對(duì)象的children列表

returnBean = FindById(id, jsonString, bean.children);

}

//如果找到,就跳出尋找

if (returnBean != null) {

break;

}

}

return returnBean;

}

}

3.使用實(shí)例:

//假設(shè)json的字符串在變量jsonString中

MyBean bean = MyHelper.FindById("33", jsonString);

if (bean != null) {

//使用查找出來的bean

}

分享標(biāo)題:josn常用函數(shù)c語(yǔ)言 join函數(shù)c語(yǔ)言
分享路徑:http://www.bm7419.com/article10/ddioogo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站制作靜態(tài)網(wǎng)站、網(wǎng)站改版、網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司

廣告

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

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