當前位置:首頁 » 編程語言 » c語言json

c語言json

發布時間: 2022-01-08 07:53:25

c語言的cjson怎樣處理轉義

的JSON數據格式有問題,所有Name鍵的值後面少了單引號, 應該修改形如這樣格式的字元

⑵ 如何從c語言層返回 json 的數據結構

想要很輕松的學會數據結構就要把C語言的語法記得死死的。書里關於演算法的思想其實都是很容易理解的,在老師的講解下很好明白,但是切記不要把演算法等同於程序,這是學習這門課的一個很簡單的大忌,在理解思想的基礎上再開始看演算法,注意這時一定要靈活,不要拘於C語言的語法規則。不管是看說明圖還是看演算法,在大徹大悟之後就可以試著編代碼了,這是最麻煩的一步,但成功後的喜悅是很讓人嚮往的。

⑶ C語言讀取多行json文件數據 用哪種庫比較好, 具體怎麼操作

有的是 下面是超市 請自選 JSON_checker. YAJL. js0n. LibU. json-c. json-parser. jsonsl. WJElement. M's JSON parser. cJSON. Jansson. jsmn. cson. parson. ujson4c. nxjson.

⑷ 用C語言解析JSON數據

http://www.json.org/
列出了一堆C語言的JSON庫。

C:
JSON_checker.
YAJL.
js0n.
LibU.
json-c.
json-parser.
jsonsl.
WJElement.
M's JSON parser.
cJSON.
Jansson.
jsmn.
cson.
parson.
ujson4c.
nxjson.
frozen.

⑸ C語言可以直接把資料庫查詢出來的數據變成json格式嗎,有直接的庫,可以調用嗎

有的是 下面是超市 請自選

JSON_checker.
YAJL.
js0n.
LibU.
json-c.
json-parser.
jsonsl.
WJElement.
M's JSON parser.
cJSON.
Jansson.
jsmn.
cson.
parson.
ujson4c.
nxjson.

⑹ c語言如何判斷一段字元串是否是json格式的

如果你的json 不算復雜的話,可以直接用一個簡單的正則

string pattern=@"{("\w+":(\d+|"\w+"|true|false|null))+}\] "; // input 是json字元串 var match = Regex.Match(input, pattern);

如果復雜的,你需要 判斷 ":"等!
也可以用這個:

var serializer = new JavaScriptSerializer();dynamic obj = serializer.Deserialize(json, typeof(object));

//判斷 obj就行!

⑺ 怎麼用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字元串

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

⑼ c語言中如何將一個JSON添加到Array中

c語言沒有提供這么高級的操作,你只能用循環來添加

熱點內容
圖形化編程語言 發布:2024-09-21 09:13:14 瀏覽:417
python二維 發布:2024-09-21 08:40:05 瀏覽:231
安卓電視上如何下載電視家 發布:2024-09-21 08:29:57 瀏覽:293
php字元串引號 發布:2024-09-21 08:17:03 瀏覽:783
androidduration 發布:2024-09-21 08:17:03 瀏覽:402
大話西遊2華山論劍什麼時候開的伺服器 發布:2024-09-21 08:00:15 瀏覽:530
編程馬鞍數 發布:2024-09-21 07:48:32 瀏覽:729
新建文件夾dos命令 發布:2024-09-21 07:44:13 瀏覽:132
舞蹈解壓介紹 發布:2024-09-21 07:40:04 瀏覽:974
qq如何顯示密碼 發布:2024-09-21 07:22:26 瀏覽:567