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 编辑过 |
关键字查询
相关文章
正在查询... |