導航:首頁 > 編程語言 > c解析一個json數組

c解析一個json數組

發布時間:2023-06-26 08:28:54

❶ 怎麼用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;

}

❷ json對象中含有數組,數組中又含有數組如何用C#解析求實例

c#中解析json對象建議你使用Newtonsoft.Json

你可以用nuget獲得

usingNewtonsoft.Json;

varjsonStr="";
varresult=JsonConvert.DeserializeObject<Model>(jsonStr);

其中Model可以自動生成

❸ cjson數組如何解析

JSONObject Gson Xstream等等的類庫都可以解析
請把你的json代碼提交上來JSONObject object = new JSONObject(json);JSONObject root = object.getJSONObject("root"); JSONObject data = root.getJSONObject("data"); JSONArray list = data.getJSONArray("list");for(int i=0; i<list.length(); i++){ JSONObject entityObj = list.getJSONObject(i); JSONArray entitys = entityObj.getJSONArray("entity"); for(int j=0; j<entitys.length(); j++){ JSONObject entity = entitys.getJSONObject(j); String date = entity.getString("取消日期"); } }引入jar,替換你的json直接可運行

❹ c#解析JSON的幾種辦法

對比





❺ C#解析json數組,查詢是否有某個值得鍵。

你的json格式有點問題,應該是不完整且鍵名不能相同。

下面我寫了個相同的程序,希望能符合你的要求,代碼如下:

publicclassKeysInfo{

publicKeysInfo(){}

[JsonIgnore]
publicstringTest{get;set;}

publicMyKeys[]mykeys{get;set;}
}

publicclassMyKeys
{
publicstringkey0{get;set;}
publicstringkey1{get;set;}
publicstringkey2{get;set;}
publicstringkey3{get;set;}
publicstringkey4{get;set;}
publicstringkey5{get;set;}
publicstringkey6{get;set;}
publicstringkey7{get;set;}

}

classProgram
{
staticvoidMain(string[]args)
{
stringmyJson="{"Test":"2015","MyKeys":[{"key0":"a","key1":"b","key2":"c","key3":"d"},{"key4":"aa","key5":"bb","key6":"cc","key7":"dd"}]}";
//反序列化json對象
KeysInfodes=JsonConvert.DeserializeObject<KeysInfo>(myJson);

if(IsExist(des,"key4","aa"))Console.WriteLine("存在. ");
elseConsole.WriteLine("不存在. ");

Console.ReadKey(true);
}

publicstaticboolIsExist(KeysInfokeyInfo,stringkeyName,stringkeyValue)
{
for(inti=0;i<keyInfo.mykeys.Count();i++)
{
//反射KeysInfo類的屬性以及值
foreach(System.Reflection.PropertyInfopiinkeyInfo.mykeys[i].GetType().GetProperties())
{
if(pi.Name==keyName&&(pi.GetValue(keyInfo.mykeys[i])!=null
&&pi.GetValue(keyInfo.mykeys[i]).ToString()==keyValue))
returntrue;//給定的鍵名與值存在於json對象
}
}
returnfalse;//給定的鍵名與值不存在於json對象
}
}

❻ 這種json怎麼解析[ "1", "2", "3", "4" ]

int cjson_parse(char *json_data)
{
cJSON *json,*item;
int i;
json=cJSON_Parse(json_data);
if (!json)
{
printf("Error before: [%s]\n",cJSON_GetErrorPtr());
return -1;
}
else
{
int size=cJSON_GetArraySize(json);
for(i=0;i<size;i++)
{
item=cJSON_GetArrayItem(json,i);

//atoi(item->valuestring)等到整數,

}

}

}
————————————————
版權聲明:本文為CSDN博主「hairubb」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/hairubb/article/details/102778242

❼ c#後台解析json數組,該怎麼解決

使用開源的類庫Newtonsoft.Json(下載地址http://json.codeplex.com/)。下載後加入工程就能用。通常可以使用JObject, JsonReader, JsonWriter處理。這種方式最通用,也最靈活,可以隨時修改不爽的地方。
string jsonArrayText1 = "[{'a':'a1','b':'b1'},{'a':'a2','b':'b2'}]";
JArray ja = (JArray)JsonConvert.DeserializeObject(jsonArrayText1);
string ja1a = ja[1]["a"].ToString();
//或者
JObject o = (JObject)ja[1];
string oa = o["a"].ToString();

❽ JSON中數組該如何解析呢c++中使用jsoncpp

JSON是一個輕量級的數據定義格式,比起XML易學易用,而擴展功能不比XML差多少,用之進行數據交換是一個很好的選擇
JSON的全稱為:javaScript Object Notation ,顧名思義,JSON是用於標記javascript對象的,詳情參考http://www.json.org/。
本文選擇第三方庫JsonCpp來解析json,JsonCpp是比較出名的c++解析庫,在json官網也是首推的。
JsonCpp簡介
JsonCpp主要包含三種類型的class:Value Reader Writer。
jsoncpp中所有對象、類名都在namespace json中,包含json.h即可。
注意: Json::Value只能處理ANSI類型的字元串,如果C++程序使用Unicode編碼的,最好加一個Adapt類來適配。
下載和編譯
本文運行環境是: Redhat 5.5 + g++version 4.6.1 + GNU Make 3.81 + jsoncpp-0.5.0
下載地址是:http://sourceforge.net/projects/jsoncpp/
解壓之後得到jsoncpp-src-0.5.0文件夾,我們只需要jsoncpp的頭文件和cpp文件,其中jsonscpp的頭文件位於jsoncpp-src-0.5.0includejson,jsoncpp的cpp文件位於jsoncpp-src-0.5.0srclib_json。
這里我列出我們的工作目錄:
jsoncpp/ //工作目錄
|-- include //頭文件根目錄
| |-- json //json頭文件,對應jsoncpp-src-0.5.0includejson
|-- src //cpp源碼文件根目錄
|-- json //jsoncpp源碼文件,對應jsoncpp-src-0.5.0srclib_json
|-- main.cpp //我們的主函數,調用jsoncpp的示例代碼
|-- makefile //makefile,不用我們多說了吧,不懂請看我博客的makefile最佳實踐
反序列化Json對象
假設有一個json對象如下:

{ "name": "json″, "array": [ { "cpp": "jsoncpp" }, { "java": "jsoninjava" }, { "php": "support" } ] }

我們要實現這個json的反序列號代碼如下:
voidreadJson() { usingnamespacestd; std::stringstrValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}"; Json::Reader reader; Json::Value value; if(reader.parse(strValue, value)) { std::stringout= value["name"].asString(); std::cout <<out<<std::endl; constJson::Value arrayObj = value["array"]; for(unsigned inti = 0;i <arrayObj.size(); i++) { if(!arrayObj[i].isMember("cpp")) continue; out= arrayObj[i]["cpp"].asString(); std::cout <<out; if(i != (arrayObj.size() - 1)) std::cout <<std::endl; } } }

序列化Json對象
voidwriteJson() { usingnamespacestd; Json::Value root; Json::Value arrayObj; Json::Value item; item["cpp"] = "jsoncpp"; item["java"] = "jsoninjava"; item["php"] = "support"; arrayObj.append(item); root["name"] = "json"; root["array"] = arrayObj; root.toStyledString(); std::stringout= root.toStyledString(); std::cout <<out<<std::endl; }

❾ 用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格式為{"a":"0","b":{"c":"0"}}這樣的啊

string str="{'nodeID':'2','text':'Gxxxxxxx','path':'1111/111/2','attributes':{'nodeType':'async'}}"; //str為你的json傳過來的字元串,我這邊調試呢,所以裡面用的'引號
str = str.Substring(1,str.Length-2);//去除兩邊的大括弧
string[] str1 = str.Split(',');//按,分組,現在str1就是把你的字元串分成了一維數組
string[] str2;
List<string[]> strs = new List<string[]>();
for (int i = 0; i < str1.Length; i++)
{
str2 = str1[i].Split(':');
strs.Add(str2);//這里分成二維數組
}


如果你不要傳過來字元串的「引號,就用replace一下

閱讀全文

與c解析一個json數組相關的資料

熱點內容
遇見不安全網站怎麼辦 瀏覽:251
哪個app有慶余年電視劇 瀏覽:420
iphone5s視頻時很黑 瀏覽:601
js獲取埠號 瀏覽:347
手機短息發的鏈接病毒蘋果手機 瀏覽:724
win10專業忘記家庭組密碼 瀏覽:176
南寧applestore幾樓 瀏覽:296
java字元串怎麼初始化 瀏覽:349
醫美哪個app好 瀏覽:768
代碼編程和機器人編程哪個好 瀏覽:875
90版本男法 瀏覽:796
win10桌面字體難看 瀏覽:848
三菱fx5u支持哪些編程 瀏覽:7
優酷在文件夾在哪裡 瀏覽:91
趣列印的數據文件是什麼 瀏覽:871
linuxjava程序 瀏覽:483
蘋果6splus數據線圖片 瀏覽:98
蘋果官網數據如何恢復 瀏覽:251
spss怎麼拆分數據 瀏覽:306
photoshop復位所有工具 瀏覽:492

友情鏈接