使用 c# 反序列化操作

Posted

技术标签:

【中文标题】使用 c# 反序列化操作【英文标题】:Deserialize operations with c# 【发布时间】:2022-01-01 09:57:37 【问题描述】:

像下面这样的数据来自 API。我该如何建模?


   "USD":
      "satis":"11.4995",
      "alis":"11.4012",
      "degisim":"1,90"
   ,
   "EUR":
      "satis":"12.9252",
      "alis":"12.8265",
      "degisim":"1,37"
   ,
   "GBP":
      "satis":"15.4003",
      "alis":"15.2761",
      "degisim":"1,44"
   ,
   "CHF":
      "satis":"12.3345",
      "alis":"12.2252",
      "degisim":"1,47"
   ,
   "CAD":
      "satis":"9.0611",
      "alis":"8.9809",
      "degisim":"1,58"
   ,
   "RUB":
      "satis":"0.15338",
      "alis":"0.15207",
      "degisim":"-0,14"
   ,
   "AED":
      "satis":"3.1297",
      "alis":"3.1052",
      "degisim":"1,88"
   ,
   "AUD":
      "satis":"8.3101",
      "alis":"8.2403",
      "degisim":"1,79"
   ,
   "DKK":
      "satis":"1.7381",
      "alis":"1.723",
      "degisim":"1,47"
   ,
   "SEK":
      "satis":"1.2798",
      "alis":"1.2685",
      "degisim":"1,52"
   ,
   "NOK":
      "satis":"1.2882",
      "alis":"1.2768",
      "degisim":"1,79"
   ,
   "JPY":
      "satis":"10.0053",
      "alis":"9.9214",
      "degisim":"1,05"
   ,
   "KWD":
      "satis":"37.9655",
      "alis":"37.7194",
      "degisim":"1,97"
   ,
   "ZAR":
      "satis":"0.7253",
      "alis":"0.72",
      "degisim":"1,20"
   ,
   "OMR":
      "satis":"29.7636",
      "alis":"29.7266",
      "degisim":"1,80"
   ,
   "MYR":
      "satis":"2.7464",
      "alis":"2.7261",
      "degisim":"1,83"
   ,
   "php":
      "satis":"0.2267",
      "alis":"0.225",
      "degisim":"1,57"
   ,
   "QAR":
      "satis":"3.1567",
      "alis":"3.1329",
      "degisim":"2,54"
   ,
   "SYP":
      "satis":"0.0046",
      "alis":"0.0045",
      "degisim":"2,22"
   ,
   "RSD":
      "satis":"0.1098",
      "alis":"0.1091",
      "degisim":"1,48"
   ,
   "UAH":
      "satis":"0.4286",
      "alis":"0.4256",
      "degisim":"1,06"
   ,
   "TWD":
      "satis":"0.4133",
      "alis":"0.4102",
      "degisim":"1,85"
   ,
   "MDL":
      "satis":"0.6473",
      "alis":"0.6464",
      "degisim":"1,62"
   ,
   "PEN":
      "satis":"2.8543",
      "alis":"2.8481",
      "degisim":"1,78"
   ,
   "SGD":
      "satis":"8.4228",
      "alis":"8.3574",
      "degisim":"1,63"
   ,
   "MKD":
      "satis":"0.2088",
      "alis":"0.209",
      "degisim":"1,02"
   ,
   "UYU":
      "satis":"0.2611",
      "alis":"0.2592",
      "degisim":"2,23"
   ,
   "PKR":
      "satis":"0.0653",
      "alis":"0.0654",
      "degisim":"1,40"
   ,
   "THB":
      "satis":"0.348",
      "alis":"0.3454",
      "degisim":"1,34"
   ,
   "SAR":
      "satis":"3.0694",
      "alis":"3.0359",
      "degisim":"2,15"
   ,
   "ILS":
      "satis":"3.7246",
      "alis":"3.6967",
      "degisim":"2,19"
   ,
   "BHD":
      "satis":"30.2794",
      "alis":"30.461",
      "degisim":"1,91"
   ,
   "MXN":
      "satis":"0.5477",
      "alis":"0.5438",
      "degisim":"1,22"
   ,
   "NZD":
      "satis":"7.9952",
      "alis":"7.933",
      "degisim":"1,31"
   ,
   "IRR":
      "satis":"0.0003",
      "alis":"0.0003",
      "degisim":"0,00"
   ,
   "CSK":
      "satis":"0.5075",
      "alis":"0.5037",
      "degisim":"1,30"
   ,
   "BGN":
      "satis":"6.6045",
      "alis":"6.555",
      "degisim":"1,42"
   ,
   "ARS":
      "satis":"0.1145",
      "alis":"0.1136",
      "degisim":"1,96"
   ,
   "BRL":
      "satis":"2.0518",
      "alis":"2.0356",
      "degisim":"2,02"
   ,
   "AZN":
      "satis":"6.763",
      "alis":"6.7081",
      "degisim":"1,88"
   ,
   "CLP":
      "satis":"0.0142",
      "alis":"0.0141",
      "degisim":"4,41"
   ,
   "RON":
      "satis":"2.609",
      "alis":"2.591",
      "degisim":"1,43"
   ,
   "CRC":
      "satis":"0.0178",
      "alis":"0.018",
      "degisim":"1,71"
   ,
   "HKD":
      "satis":"1.4759",
      "alis":"1.4642",
      "degisim":"1,90"
   ,
   "BAM":
      "satis":"6.5939",
      "alis":"6.6078",
      "degisim":"1,69"
   ,
   "ISK":
      "satis":"0.0872",
      "alis":"0.0867",
      "degisim":"1,28"
   ,
   "KZT":
      "satis":"0.0265",
      "alis":"0.0263",
      "degisim":"1,92"
   ,
   "LKR":
      "satis":"0.0566",
      "alis":"0.0565",
      "degisim":"1,80"
   ,
   "DZD":
      "satis":"0.0825",
      "alis":"0.0822",
      "degisim":"2,23"
   ,
   "MAD":
      "satis":"1.244",
      "alis":"1.2347",
      "degisim":"1,45"
   ,
   "JOD":
      "satis":"16.159",
      "alis":"16.1411",
      "degisim":"1,88"
   ,
   "LTL":
      "satis":"0.976",
      "alis":"0.974",
      "degisim":"0,00"
   ,
   "LYD":
      "satis":"2.4832",
      "alis":"2.4899",
      "degisim":"1,60"
   ,
   "INR":
      "satis":"0.1545",
      "alis":"0.1533",
      "degisim":"1,78"
   ,
   "IDR":
      "satis":"0.0008",
      "alis":"0.0008",
      "degisim":"0,00"
   ,
   "IQD":
      "satis":"0.0079",
      "alis":"0.0078",
      "degisim":"2,60"
   ,
   "CNY":
      "satis":"1.8002",
      "alis":"1.7863",
      "degisim":"1,90"
   ,
   "HUF":
      "satis":"0.0349",
      "alis":"0.0346",
      "degisim":"0,87"
   ,
   "BYR":
      "satis":"0.0002",
      "alis":"0.0002",
      "degisim":"0,00"
   ,
   "PLN":
      "satis":"2.743",
      "alis":"2.7239",
      "degisim":"1,06"
   ,
   "EGP":
      "satis":"0.7295",
      "alis":"0.7276",
      "degisim":"1,81"
   ,
   "ALL":
      "satis":"0.1063",
      "alis":"0.1059",
      "degisim":"1,43"
   ,
   "KRW":
      "satis":"0.0097",
      "alis":"0.0096",
      "degisim":"2,11"
   ,
   "COP":
      "satis":"0.0029",
      "alis":"0.0029",
      "degisim":"0,00"
   ,
   "LVL":
      "satis":"5.4364",
      "alis":"5.4011",
      "degisim":"0,00"
   ,
   "HRK":
      "satis":"1.7179",
      "alis":"1.7051",
      "degisim":"1,36"
   ,
   "LBP":
      "satis":"0.0076",
      "alis":"0.0076",
      "degisim":"2,70"
   ,
   "GEL":
      "satis":"3.6714",
      "alis":"3.6494",
      "degisim":"1,77"
   ,
   "DVZSP1":
      "satis":"12.2124",
      "alis":"12.1139",
      "degisim":"1,62"
   ,
   "TND":
      "satis":"4.0186",
      "alis":"3.9901",
      "degisim":"46,69"
   

我的 api 代码:

var client = new RestSharp.RestClient("https://api.genelpara.com/embed/doviz.json");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
var veri = JsonSerializer.Deserialize<DigerApiDoviz>(response.Content);

我的模型:

public class DigerApiDoviz

    public Doviz doviz  get; set; 


public class Doviz

    public decimal satis  get; set; 
    public decimal alis  get; set; 
    public decimal degisim  get; set; 

虽然接收到数据,但在“反序列化”操作中返回空值。因为我把我的模型弄错了。正确的建模过程是什么?

【问题讨论】:

看起来像字典 对我来说你需要在 DigerApiDoviz 类中。 @Ralf 那么我该怎么做呢?公共字典 doviz;我试过了,但没有用 var veri = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, Doviz&gt;&gt;(response.Content); 不知道您正在使用的 JsonSerializer.Deserialize 重载。但显示的代码对我有用。 这不是NewtonSoft Json.Net Serializer吗? @Ralf 当您将 döviz 类中的属性从十进制转换为字符串时,您提到的代码有效。谢谢 【参考方案1】:

我推荐用于反序列化的字典模型和 NewtonSoft.Json 作为序列化程序,因为您在序列化方面遇到了一些问题

Dictionary<string,Doviz> veri = JsonConvert.DeserializeObject<Dictionary<string,Doviz>> (response.Content);

你可以像字典一样使用它

Doviz dv =veri["USD"];

或者您可以将其转换为列表并使用 linq

List<DigerApiDoviz> list = veri
.Select(v => new DigerApiDoviz  Name = v.Key, Doviz = v.Value ).ToList();

由于 json 只包含字符串属性,但 c# 对象应该具有小数属性,最好使用构造函数将字符串转换为小数,特别是因为 degisim 属性具有无效符号“,”

public class Doviz

        [JsonProperty("satis")]
        public decimal Satis  get; set; 
    
        [JsonProperty("alis")]
        public decimal Alis  get; set; 
    
        [JsonProperty("degisim")]
        public decimal Degisim  get; set; 

    [JsonConstructor]
    public Doviz (string satis, string alis, string degisim)
    
        Satis=Convert.ToDecimal(satis);
        Alis=Convert.ToDecimal(alis);
        Degisim= Convert.ToDecimal(degisim.Replace(",","."));
    

public class DigerApiDoviz

    public string Name  get; set; 
    public Doviz Doviz  get; set; 


【讨论】:

如果这解决了您的问题,我鼓励您“接受”它。

以上是关于使用 c# 反序列化操作的主要内容,如果未能解决你的问题,请参考以下文章

使用C# json 二维数组 反序列化

在 C# 中反序列化贝宝响应的正确方法

C#对 Json的序列化和反序列化时出现“k_BackingField”

c# 多个json字符串反序列化

使用 C# 反序列化 JSON

使用 C# 反序列化 Avro 文件