如何使用多个类修复 Json 中的反序列化 <null in object reference>

我能够反序列化根对象但无法访问基础类我收到空引用异常。我需要从 orderitems 类中提取字段。


在以下场景中,反序列化效果很好,可以提取附加到项目的字段


 var  Sresponse = JsonConvert.DeserializeObject<RootObject>(json);

                  Console.WriteLine(Sresponse.items);

但是在这里不起作用


 var  Sresponse = JsonConvert.DeserializeObject<Item>(json);

                  Console.WriteLine(Sresponse.orderitems);

错误消息:System.NullReferenceException:未将对象引用设置为对象的实例


慕工程0101907
浏览 163回答 1
1回答

白板的微信

首先,修复问题中错误定义的 JSON 字符串,然后Model在您的案例中使用此结构:注意:要解析JSON 中Property带有特殊字符(如-)的名称,您可以使用JSONProperty如下所示的属性来解析这些属性。public class Additionaldata{[JsonProperty(PropertyName = "pos-timezone")]public string postimezone { get; set; }[JsonProperty(PropertyName = "pos-staff-externalid")]public string posstaffexternalid { get; set; }}public class Orderitem{&nbsp; &nbsp; public int orderitemid { get; set; }&nbsp; &nbsp; public int orderitemtype { get; set; }&nbsp; &nbsp; public int productid { get; set; }&nbsp; &nbsp; public string productname { get; set; }&nbsp; &nbsp; public string sku { get; set; }&nbsp; &nbsp; public string productattributes { get; set; }&nbsp; &nbsp; public string externalinput { get; set; }&nbsp; &nbsp; public string externalinputtitle { get; set; }&nbsp; &nbsp; public string unitlabel { get; set; }&nbsp; &nbsp; public int quantity { get; set; }&nbsp; &nbsp; public object decimalunitquantity { get; set; }&nbsp; &nbsp; public string moneynetpriceperunit { get; set; }&nbsp; &nbsp; public string moneypriceorg { get; set; }&nbsp; &nbsp; public int vatvalue { get; set; }&nbsp; &nbsp; public string deliveryinfo { get; set; }&nbsp; &nbsp; public string moneyitemtotal_net { get; set; }&nbsp; &nbsp; public string moneyitemtotal_vat { get; set; }&nbsp; &nbsp; public int voucherid { get; set; }&nbsp; &nbsp; public string vouchercode { get; set; }&nbsp; &nbsp; public string vouchername { get; set; }&nbsp; &nbsp; public string moneyoriginalprice { get; set; }&nbsp; &nbsp; public string moneydiscountedprice { get; set; }&nbsp; &nbsp; public string moneydiscount { get; set; }&nbsp; &nbsp; public List<object> salestaxes { get; set; }&nbsp; &nbsp; public Additionaldata additionaldata { get; set; }&nbsp; &nbsp; public string decimalquantitytotal { get; set; }&nbsp; &nbsp; public string moneynetpriceperquantity { get; set; }}public class Item{&nbsp; &nbsp; public int orderid { get; set; }&nbsp; &nbsp; public string email { get; set; }&nbsp; &nbsp; public string namefirst { get; set; }&nbsp; &nbsp; public string namelast { get; set; }&nbsp; &nbsp; public string company { get; set; }&nbsp; &nbsp; public string moneyfinal_net { get; set; }&nbsp; &nbsp; public string moneyfinal_vat { get; set; }&nbsp; &nbsp; public string moneytotal_gross_roundoff { get; set; }&nbsp; &nbsp; public string moneytotal_gross_all { get; set; }&nbsp; &nbsp; public string checkouttypename { get; set; }&nbsp; &nbsp; public string deliverytypename { get; set; }&nbsp; &nbsp; public int orderdate { get; set; }&nbsp; &nbsp; public int orderstateid { get; set; }&nbsp; &nbsp; public int paymentstateid { get; set; }&nbsp; &nbsp; public int ordertypeid { get; set; }&nbsp; &nbsp; public string registerid { get; set; }&nbsp; &nbsp; public int warehouseid { get; set; }&nbsp; &nbsp; public object datereserved { get; set; }&nbsp; &nbsp; public string currencycode { get; set; }&nbsp; &nbsp; public Additionaldata additionaldata { get; set; }&nbsp; &nbsp; public List<Orderitem> orderitems { get; set; }}public class RootObject{&nbsp; &nbsp; public int totalcount { get; set; }&nbsp; &nbsp; public List<Item> items { get; set; }}最后反序列化它:using System;using Newtonsoft.Json;using System.Collections.Generic;public class Program{&nbsp; &nbsp; public static void Main()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string json=@"{'totalcount':103952,'items':[{'orderid':113951,'email':'','namefirst':'','namelast':'','company':'','moneyfinal_net':'95.92','moneyfinal_vat':'23.98','moneytotal_gross_roundoff':'0.00','moneytotal_gross_all':'119.90','checkouttypename':'Card','deliverytypename':'','orderdate':1554836745,'orderstateid':10,'paymentstateid':20,'ordertypeid':10,'registerid':'{AD16AEE2-235F-318A-4323-6B63EC2C40E7}','warehouseid':18,'datereserved':null,'currencycode':'NOK','additionaldata':{'pos-timezone':'Europe/Oslo','pos-staff-externalid':'4654'},'orderitems':[{'orderitemid':0,'orderitemtype':10,'productid':5486,'productname':'Test','sku':'320991800016','productattributes':'','externalinput':'','externalinputtitle':'','unitlabel':'ST','quantity':1,'decimalunitquantity':null,'moneynetpriceperunit':'63.92','moneypriceorg':'0.00','vatvalue':25,'deliveryinfo':'','moneyitemtotal_net':'63.92','moneyitemtotal_vat':'15.98','voucherid':0,'vouchercode':'','vouchername':'','moneyoriginalprice':'63.92','moneydiscountedprice':'0.00','moneydiscount':'0.00','salestaxes':[],'additionaldata':{},'decimalquantitytotal':'1.000','moneynetpriceperquantity':'63.92'}]}]}";&nbsp; &nbsp; &nbsp; &nbsp; var&nbsp; Sresponse = JsonConvert.DeserializeObject<RootObject>(json);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(Sresponse.totalcount);&nbsp; &nbsp; &nbsp; &nbsp; foreach(var result in Sresponse.items)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine(result.moneyfinal_net);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.WriteLine(result.additionaldata.postimezone);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;foreach(var result1 in result.orderitems)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(result1.orderitemid);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(result1.orderitemtype);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(result1.productid);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(result1.productname);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(result1.sku);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }}输出:10395295.92Europe/Oslo0105486Test320991800016工作示例:https ://dotnetfiddle.net/kGXBQ0
打开App,查看更多内容
随时随地看视频慕课网APP