簡體   English   中英

第一次使用 NewtonSoft (JsonConvert.DeserializeObject<>() 反序列化 JSON 與 System.Text.Json (JsonSerializer.Deserialize<>()

[英]First time using NewtonSoft (JsonConvert.DeserializeObject<>() for deserializing JSON versus System.Text.Json (JsonSerializer.Deserialize<>()

使用 VS 2019、Dot Net Core、Azure 函數。 Http觸發器。

我之前在 .Net 桌面和服務器應用程序開發中使用過 JSON 文件。 所以,不知道AzureNewtonsoft發生了什么。 當我嘗試轉儲 wht.YourName.last 的值時,出現錯誤,

你調用的對象是空的

這讓我感到困惑,因為在我使用System.Text.Json其他編程中,如果我有一個包含子級的 JSON 文件結構的類模型,我使用的常用函數調用是JsonSerializer.Deserialize<jsonclass>(jsonstring) ,設置父父級的子級沒有問題(參考下面的類結構)。

兩個函數實現JsonConvert.DeserializeObjectJsonSerializer.Deserialize之間有區別嗎?

我看到的所有代碼示例似乎都適用於Newtonsoft 的方法,就像它在 dot net 中的對應物一樣,它不需要我對 NameBlock 類/對象進行任何預初始化。

希望我解釋得足夠好,考慮到我的大腦在學習 Azure 時有點吃力。

這是我使用 Newtonsoft 方法的反序列化代碼:

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();    
WebhookTest wht = JsonConvert.DeserializeObject<WebhookTest>(requestBody);

httprequest.Body 的原始轉儲:

{
   "FormID":"1081011",
   "UniqueID":"178165183",
   "support_request":"",
   "your_name":{
      "first":"Testy",
      "last":"Tester"
   },
   "email":"jxxxx@gxxxcxxxaxxxxxxxx.com",
   "phone":"(111) 867-5309",
   "upload_a_file_optional":"",
   "request_details":"Testing latest revision of Azure function that deserializes JSON data into a class object."
}

這是我的類結構的樣子:

public class NameBlock    
{    
public string first { get; set; }    
public string last { get; set; }             
} 

public class WebhookTest  
{
   public string FormID { get; set; }                 
   public string UniqueID { get; set; }                 
   public string SupportRequest { get; set; }                 
   public NameBlock YourName { get; set; }                 
   public string Email { get; set; }                 
   public string Phone { get; set; }  
          
   [JsonProperty("Uploadafile(optional)")]                 
   public string UploadafileOptional { get; set; }                 
   public string RequestDetails { get; set; }             
}

錯誤Object reference not set to an instance of an object是代碼找不到wht.YourName (因為wht.YourName為空)。 請將您的班級結構更改為:

public class NameBlock
{
    public string first { get; set; }
    public string last { get; set; }
}

public class WebhookTest
{
    public string FormID { get; set; }
    public string UniqueID { get; set; }
    public string SupportRequest { get; set; }

    [JsonProperty(PropertyName = "your_name")]
    public NameBlock YourName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }

    [JsonProperty("Uploadafile(optional)")]
    public string UploadafileOptional { get; set; }
    public string RequestDetails { get; set; }
}

只需添加一行[JsonProperty(PropertyName = "your_name")]

順便說一句

當我們使用System.Text.Json ,它會根據駝峰定律自動將your_name (在請求 json 中)與YourName (在類結構中)相關聯。 但是當我們使用Newtonsoft.Json ,它就做不到了。 所以它不能將your_nameYourName聯系起來。 我們需要添加[JsonProperty(PropertyName = "your_name")]來讓代碼知道如何關聯這兩個屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM