本章将和大家分享如何在ASP.NET中使用JObject和JArray解析Json数据。话不多说,下面我们直接来看一个示例。
数据样例(模拟接口返回的Json字符串),如下所示:
{ "code":1, "msg":"获取成功", "total":3, "data":[ { "StuId":"10000", "Name":"张三", "Age":18 }, { "StuId":"10001", "Name":"李四", "Age":15 }, { "StuId":"10002", "Name":"王五", "Age":19 } ], "otherinfo":{ "year":2002, "schools":[ { "name":"北京大学", "ranking":1 }, { "name":"清华大学", "ranking":2 }, { "name":"北京航空航天大学", "ranking":3 } ] } }
使用JObject和JArray解析上面的Json数据,如下所示:
using System; using System.Linq; using Newtonsoft.Json.Linq; namespace MyJObjectAndJArray { class Program { static void Main(string[] args) { Show(); } /// <summary> /// 展示 /// </summary> static void Show() { Console.WriteLine("**********************演示开始**********************"); Console.WriteLine(""); //模拟接口返回Json字符串 string strJson = "{"code":1,"msg":"获取成功","total":3,"data":[{"StuId":"10000","Name":"张三","Age":18},{"StuId":"10001","Name":"李四","Age":15},{"StuId":"10002","Name":"王五","Age":19}],"otherinfo":{"year":2002,"schools":[{"name":"北京大学","ranking":1},{"name":"清华大学","ranking":2},{"name":"北京航空航天大学","ranking":3}]}}"; //Json解析 JObject jObject = JObject.Parse(strJson); if (jObject.ContainsKey("code")) //判断是否包含某个属性名 { Console.WriteLine("包含属性名code"); } if (!jObject.ContainsKey("error")) { Console.WriteLine("不包含属性名error"); } var msg = jObject.ContainsKey("msg") ? jObject.Value<string>("msg") : "无返回信息"; Console.WriteLine($"msg={msg}"); var code1 = Convert.ToInt32(jObject["code"]); //方式1 var code2 = jObject.Value<int>("code"); //方式2 Console.WriteLine($"code1={code1} , code2={code2}"); var year1 = Convert.ToInt32(jObject["otherinfo"]["year"]); //方式1 var year2 = jObject["otherinfo"].Value<int>("year"); //方式2 Console.WriteLine($"year1={year1} , year2={year2}"); var stuId1 = Convert.ToInt32(jObject["data"][0]["StuId"]); var stuId2 = jObject["data"][1].Value<int>("StuId"); JArray data1 = jObject.Value<JArray>("data") ?? new JArray(); //方式1 var stuId3 = Convert.ToInt32(data1[2]["StuId"]); JArray data2 = (JArray)jObject["data"] ?? new JArray(); //方式2 var stuId4 = data2[2].Value<int>("StuId"); Console.WriteLine($"stuId1={stuId1} , stuId2={stuId2} , stuId3={stuId3} , stuId4={stuId4} , data1.Count={data1.Count}"); if (jObject.Value<int>("code") == 1) { var apiData = (jObject.Value<JArray>("data") ?? new JArray()) .Select(m => new { StuId = m.Value<int>("StuId"), Name = m.Value<string>("Name"), Age = m.Value<int>("Age"), }) .ToList(); Console.WriteLine($"apiData.Count={apiData.Count}"); var apiSchools = (jObject["otherinfo"].Value<JArray>("schools") ?? new JArray()) .Select(m => new { name = m.Value<string>("name"), ranking = m.Value<int>("ranking") }) .ToList(); Console.WriteLine($"apiSchools.Count={apiSchools.Count}"); JObject joOtherInfo = (JObject)jObject["otherinfo"]; if (joOtherInfo.ContainsKey("schools")) { var apiSchools2 = (joOtherInfo.Value<JArray>("schools") ?? new JArray()) .Select(m => new { name = m.Value<string>("name"), ranking = m.Value<int>("ranking") }) .ToList(); Console.WriteLine($"apiSchools2.Count={apiSchools2.Count}"); } } Console.WriteLine(""); Console.WriteLine("**********************演示结束**********************"); Console.ReadKey(); } } }
PS:项目中需要引入Newtonsoft.Json程序包。
运行结果如下:
至此本文就全部介绍完了,如果觉得对您有所启发请记得点个赞哦!!!
Demo源码:
链接:https://pan.baidu.com/s/17K_zqhPSvdj7GlXvekWaYQ 提取码:grux
此文由博主精心撰写转载请保留此原文链接:https://www.cnblogs.com/xyh9039/p/14477480.html
版权声明:如有雷同纯属巧合,如有侵权请及时联系本人修改,谢谢!!!