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