C#解析JSON
當(dāng)前位置:點(diǎn)晴教程→知識管理交流
→『 技術(shù)文檔交流 』
1. JSON對象解析,只包含一層對象{}{ "name":"張三", "name_en":"zhangssan" } string jsonText = "{\"name\":\"張三\",\"name_en\":\"zhangssan\"}"; //解析對象JObject JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText); //獲取此對象key="name"和key="name_en"的值 string zone = jo["name"].ToString(); string zone_en = jo["name_en"].ToString(); Console.WriteLine(zone); Console.WriteLine(zone_en); 2. 嵌套JSON對象解析,包含多層對象{}{ "allname":{ "name":"張三", "name_en":"zhangsan" } } string jsonText = "{\"allname\":{\"name\":\"張三\",\"name_en\":\"zhangsan\"}}"; //解析對象JObject JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText); //獲取此對象中key="allname"下key="name"的值 string zone = jo["allname"]["name"].ToString(); string zone_en = jo["allname"]["name_en"].ToString(); Console.WriteLine(zone); Console.WriteLine(zone_en); 3. JSON數(shù)組解析1(數(shù)組循環(huán)遍歷)[ { "a":"a1", "b":"b1" }, { "a":"a2", "b":"b2" } ] string jsonArrayText = "[{\"a\":\"a1\",\"b\":\"b1\"},{\"a\":\"a2\",\"b\":\"b2\"}]"; //解析數(shù)組JArray JArray jArray = (JArray)JsonConvert.DeserializeObject(jsonArrayText); //循環(huán)獲取數(shù)組中的內(nèi)容 for(int i = 0; i < jArray.Count; i++) { //解析對象JObject JObject j = JObject.Parse(jArray[i].ToString()); //獲取此對象key="a"的值 string a = j["a"].ToString(); Console.WriteLine(a); } 4. JSON數(shù)組解析2(使用index獲取數(shù)據(jù),index從0開始計(jì)算)[ { "a":"a1", "b":"b1" }, { "a":"a2", "b":"b2" } ] string jsonArrayText = "[{\"a\":\"a1\",\"b\":\"b1\"},{\"a\":\"a2\",\"b\":\"b2\"}]"; //解析數(shù)組JArray JArray jArray = (JArray)JsonConvert.DeserializeObject(jsonArrayText); //獲取數(shù)組jArray中第1個(gè)(從第0開始計(jì)算)key="a"的值 string str = jArray[1]["a"].ToString(); Console.WriteLine(str); 5. JSON 對象中的數(shù)組,一層對象{}中包含數(shù)組[]{ "name":"網(wǎng)站", "num":3, "sites":[ "Google", "Runoob", "Taobao" ] } string jsonText = "{\"name\":\"網(wǎng)站\", \"num\":3, \"sites\":[\"Google\",\"Runoob\",\"Taobao\"]}"; //解析對象JObject JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText); string sites = jo["sites"].ToString(); //解析數(shù)組JArray JArray jArray = (JArray)JsonConvert.DeserializeObject(sites); //獲取數(shù)組sites jArray中第0個(gè)(從第0開始計(jì)算)的值 string sitesdetails = jArray[0].ToString(); Console.WriteLine(sitesdetails); 6. 嵌套 JSON 對象中的數(shù)組,多層對象{}包含數(shù)組[]{ "name":"網(wǎng)站", "num":2, "sites": [ { "name":"Google", "info":[ "Android", "Google 搜索", "Google 翻譯" ] }, { "name":"Taobao", "info":[ "淘寶", "網(wǎng)購" ] } ] } string jsonText = "{\"name\":\"網(wǎng)站\",\"num\":2,\"sites\":[{\"name\":\"Google\",\"info\":[\"Android\",\"Google搜索\",\"Google翻譯\"]},{\"name\":\"Taobao\",\"info\":[\"淘寶\",\"網(wǎng)購\"]}]}"; //解析對象JObject JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText); string sites = jo["sites"].ToString(); //解析數(shù)組JArray JArray jArray = (JArray)JsonConvert.DeserializeObject(sites); //循環(huán)遍歷sites數(shù)組 for (int i = 0; i < jArray.Count; i++) { //解析對象JObject JObject j = JObject.Parse(jArray[i].ToString()); string info = j["info"].ToString(); //解析數(shù)組JArray JArray jArray2 = (JArray)JsonConvert.DeserializeObject(info); //循環(huán)遍歷info數(shù)組 for (int k = 0; k < jArray2.Count; k++) { Console.WriteLine(jArray2[k]); } } 該文章在 2023/10/27 15:28:26 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |