Json.Net如何将JSON数组反序列化为对象?我有一个有效的JSON对象,里面有一个JSON数组。JSON数组没有大括号,并且包含一个以逗号分隔的混合类型列表。它看起来像这样:{“ID”:17,“Days”:979,“Start_Date”:“10/13/2012”,“End_Date”:“11/12/2012”,“State”:“”,“Page":1,"Test":"Valid","ROWS":[[441210,"A","12/31/2009","OK","Done","KELLEYandCo'","12/31/2009","06/29/2010","TEXAS","Lawyers",6,"",""],[441151,"B","12/31/2009","No","In-process","Sage&Co","12/31/2009","06/29/2010","CALIFORNIA","Realtor",6,"",""]]}我创建了Created一个反映具有复杂数组列表的JSON结构的类:classmyModel{publicintID{get;放;}publicintDays{get;放;}publicDateTimeStartDate{get;放;}publicDateTimeEndDate{}publicstringState{get;放;}publicstringPage{get;放;}公共字符串测试{得到;放;}列出行{get;列表<列表>行{get;放;}像这样的子模型:classChildModel{publicintID{get;放;}publicstringStatusId{得到;放;}publicDateTimeContactDate{get;放;}公共字符串状态{得到;放;}公共字符串状态{得到;放}publicstringCustomerName{get;放;}publicDateTimeWorkStartDate{get;放;}publicDateTimeWorkEndDate{get;放;}publicstringTerritory{get;放;}publicstringCustType{get;}publicstringFiller{get;放;}公共字符串链接{得到;放;在我的program.cs文件中,我将其反序列化为:当我运行这个时,子对象(行)总是空的我做错了什么?Json.Net没有自动将数组映射到类的功能。为此,您需要一个自定义的JsonConverter。这是适合您的通用转换器。它使用自定义[JsonArrayIndex]属性来标识类中的哪些属性对应于数组中的哪些索引。如果JSON更改,这将允许您轻松更新模型。此外,您可以安全地省略不需要的类中的属性,例如Filler。这是代码:publicclassJsonArrayIndexAttribute:Attribute{publicintIndex{get;私有集;}publicJsonArrayIndexAttribute(intindex){Index=index;}}publicclassArrayToObjectConverter:JsonConverter其中T:class,new(){publicoverrideboolCanConvert(TypeobjectType){returnobjectType==typeof(T);}publicoverrideobjectReadJson(JsonReaderreader,TypeobjectType,objectexistingValue,JsonSerializerserializer){JArrayarray=JArray.Load(reader);varpropsByIndex=typeof(T).GetProperties().Where(p=>p.CanRead&&p.CanWrite&&p.GetCustomAttribute()!=null).ToDictionary(p=>p.GetCustomAttribute().Index);JObjectobj=newJObject(array.Select((jt,i)=>{PropertyInfoprop;returnpropsByIndex.TryGetValue(i,outprop)?newJProperty(prop.Name,jt):null;}).Where(jp=>jp!=null));T目标=新T();serializer.Populate(obj.CreateReader(),target);返回目标;}公共覆盖布尔CanWrite{得到{返回假;}}publicoverridevoidWriteJson(JsonWriterwriter,objectvalue,JsonSerializerserializer){thrownewNotImplementedException();要使用转换器,您需要像这样标记ChildModel类:[JsonConverter(typeof(ArrayToObjectConverter))]classChildModel{[JsonArrayIndex(0)]publicintID{get;放;}[JsonArrayIndex(1)]publicstringStatusId{get;放;}[JsonArrayIndex(2)]publicDateTimeContactDate{get;放;}[JsonArrayIndex(3)]publicstringState{get;放;}[JsonArrayIndex(4)]publicstringStatus{get;放;}[JsonArrayIndex(5)]publicstringCustomerName{get;放;}[JsonArrayIndex(6)]publicDateTimeWorkStartDate{get;放;}[JsonArrayIndex(7)]publicDateTimeWorkEndDate{get;放;}[JsonArrayIndex(8)]publicstringTerritory{get;}[JsonArrayIndex(10)]publicintJobOrder{get;放;}[JsonArrayIndex(12)]publicstringLink{get;放;然后像往常一样反序列化,它应该按照你想要的方式工作这是一个演示:https://dotnetfiddle.net/n3oE3L注意:我没有实现WriteJson,所以如果你将模型序列化为JSON,它不会序列化为数组格式;相反,它将使用默认的对象序列化。使用它将数据反序列化为json对象...publicclassRootobject{publicintID{get;放;}publicintDays{get;放;}publicstringStart_Date{get;放;}publicstringEnd_Date{get;放;}公共字符串状态{得到;放;}publicintPage{得到;放;}公共字符串测试{得到;放;}publicobject[][]ROWS{get;Convertobject[][]tothetargetobjectyouwant...(使用visualstudio创建:编辑->选择性粘贴->JSON类)以上是C#学习教程:如何使用Json.Net反序列化JSON数组为了对象?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
