第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

如何使用多個(gè)類修復(fù) Json 中的反序列化 <null in object reference>

如何使用多個(gè)類修復(fù) Json 中的反序列化 <null in object reference>

C#
慕工程0101907 2022-12-04 13:01:48
我能夠反序列化根對象但無法訪問基礎(chǔ)類我收到空引用異常。我需要從 orderitems 類中提取字段。在以下場景中,反序列化效果很好,可以提取附加到項(xiàng)目的字段 var  Sresponse = JsonConvert.DeserializeObject<RootObject>(json);                  Console.WriteLine(Sresponse.items);但是在這里不起作用 var  Sresponse = JsonConvert.DeserializeObject<Item>(json);                  Console.WriteLine(Sresponse.orderitems);錯(cuò)誤消息:System.NullReferenceException:未將對象引用設(shè)置為對象的實(shí)例
查看完整描述

1 回答

?
白板的微信

TA貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個(gè)贊

首先,修復(fù)問題中錯(cuò)誤定義的 JSON 字符串,然后Model在您的案例中使用此結(jié)構(gòu):


注意:要解析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

{

    public int orderitemid { get; set; }

    public int orderitemtype { get; set; }

    public int productid { get; set; }

    public string productname { get; set; }

    public string sku { get; set; }

    public string productattributes { get; set; }

    public string externalinput { get; set; }

    public string externalinputtitle { get; set; }

    public string unitlabel { get; set; }

    public int quantity { get; set; }

    public object decimalunitquantity { get; set; }

    public string moneynetpriceperunit { get; set; }

    public string moneypriceorg { get; set; }

    public int vatvalue { get; set; }

    public string deliveryinfo { get; set; }

    public string moneyitemtotal_net { get; set; }

    public string moneyitemtotal_vat { get; set; }

    public int voucherid { get; set; }

    public string vouchercode { get; set; }

    public string vouchername { get; set; }

    public string moneyoriginalprice { get; set; }

    public string moneydiscountedprice { get; set; }

    public string moneydiscount { get; set; }

    public List<object> salestaxes { get; set; }

    public Additionaldata additionaldata { get; set; }

    public string decimalquantitytotal { get; set; }

    public string moneynetpriceperquantity { get; set; }

}


public class Item

{

    public int orderid { get; set; }

    public string email { get; set; }

    public string namefirst { get; set; }

    public string namelast { get; set; }

    public string company { get; set; }

    public string moneyfinal_net { get; set; }

    public string moneyfinal_vat { get; set; }

    public string moneytotal_gross_roundoff { get; set; }

    public string moneytotal_gross_all { get; set; }

    public string checkouttypename { get; set; }

    public string deliverytypename { get; set; }

    public int orderdate { get; set; }

    public int orderstateid { get; set; }

    public int paymentstateid { get; set; }

    public int ordertypeid { get; set; }

    public string registerid { get; set; }

    public int warehouseid { get; set; }

    public object datereserved { get; set; }

    public string currencycode { get; set; }

    public Additionaldata additionaldata { get; set; }

    public List<Orderitem> orderitems { get; set; }

}


public class RootObject

{

    public int totalcount { get; set; }

    public List<Item> items { get; set; }

}

最后反序列化它:


using System;

using Newtonsoft.Json;

using System.Collections.Generic;


public class Program

{

    public static void Main()

    {

        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'}]}]}";

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

        Console.WriteLine(Sresponse.totalcount);


        foreach(var result in Sresponse.items)

        {    

         Console.WriteLine(result.moneyfinal_net);

         Console.WriteLine(result.additionaldata.postimezone);

         foreach(var result1 in result.orderitems)

          {

            Console.WriteLine(result1.orderitemid);

            Console.WriteLine(result1.orderitemtype);

            Console.WriteLine(result1.productid);

            Console.WriteLine(result1.productname); 

            Console.WriteLine(result1.sku);                     

          }

       }

    }

}

輸出:


103952

95.92

Europe/Oslo

0

10

5486

Test

320991800016

工作示例:https ://dotnetfiddle.net/kGXBQ0


查看完整回答
反對 回復(fù) 2022-12-04
  • 1 回答
  • 0 關(guān)注
  • 131 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號