【问题标题】:Json data Convert to C# ClassJson 数据转换为 C# 类
【发布时间】:2018-10-27 14:23:09
【问题描述】:

我有以下需要反序列化的 JSON:

 [
      {
        "Facility": "H&S 01",
        "Address": [ "5999 Cerritos Ave." ],
        "City": [ "anaheim" ],
        "State": [ "ca" ],
        "ZipCode": [ 92831 ],
        "AQDMID": [ 1 ],
        "Jan": 222.0,
        "Feb": 434.0,
        "March": 343.0,
        "April": 431.0,
        "May": 222.0,
        "June": 345.0,
        "July": 666.0,
        "Aug": 643.0,
        "Sep": 0.0,
        "Oct": 0.0,
        "Nov": 0.0,
        "Dec": 0.0,
        "Total": 3306.0
      },
      {
        "Facility": "H&S 02",
        "Address": [ "1515 N. Garey Ave." ],
        "City": [ "hshsh" ],
        "State": [ "ca" ],
        "ZipCode": [ 92831 ],
        "AQDMID": [ 2 ],
        "Jan": 122.0,
        "Feb": 234.0,
        "March": 234.0,
        "April": 263.0,
        "May": 234.0,
        "June": 124.0,
        "July": 223.0,
        "Aug": 444.0,
        "Sep": 122.0,
        "Oct": 211.0,
        "Nov": 343.0,
        "Dec": 423.0,
        "Total": 2977.0
      }
    ]

我使用http://jsontotable.com/ 创建类来表示我的数据并得到以下结果:

public class Root
{
    public DateTime Facility {get;set;}
    public List<string> Address {get;set;}
    public List<string> City {get;set;}
    public List<string> State {get;set;}
    public List<string> ZipCode {get;set;}
    public List<string> AQDMID {get;set;}
    public int Jan {get;set;}
    public int Feb {get;set;}
    public int March {get;set;}
    public int April {get;set;}
    public int May {get;set;}
    public int June {get;set;}
    public int July {get;set;}
    public int Aug {get;set;}
    public string Sep {get;set;}
    public string Oct {get;set;}
    public string Nov {get;set;}
    public string Dec {get;set;}
    public int Total {get;set;}
}

我正在尝试使用 JSON.NET 反序列化我的数据:

 List<Root> fetch = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Root>>(json);

但它不会反序列化为我的 C# 类。

【问题讨论】:

  • 所以你想降级到 C# Class Jason
  • 那个类是从哪里来的?这似乎完全错误 - 例如Facility 是一个字符串,而不是 DateTime。 Visual Studio 将为您创建类
  • 就个人而言,我坚持编写自己的类来保存我必须与之交互的任何 JSON 数据。它让我可以确保类与 JSON 描述的内容相匹配。
  • Facility 是什么日期时间?

标签: c# json asp.net-mvc


【解决方案1】:

我建议使用 json2cshap 将数据转换为 C# 类,或使用 Visual Studio 中的 Edit | Paste Special | Paste as JSON classes 菜单选项。您使用的服务似乎不能很好地猜测字段代表什么。

您的课程与您的 JSON 不匹配:

"Facility": "H&S 01",
"Facility": "H&S 02",

这些不是 DateTime 模型中声明的对象:public DateTime Facility {get;set;}

我对您的 intstring Jan-Dec 值以及您的 Total 值有点困惑。 JSON 中的所有值都是十进制的,但在 C# 模型中,它们表示为整数和字符串。我建议所有这些都使用doubledecimal

固定类可能如下所示:

public class Root
{
    public string Facility { get; set; }
    public List<string> Address { get; set; }
    public List<string> City { get; set; }
    public List<string> State { get; set; }
    public List<string> ZipCode { get; set; }
    public List<string> AQDMID { get; set; }
    public double Jan { get; set; }
    public double Feb { get; set; }
    public double March { get; set; }
    public double April { get; set; }
    public double May { get; set; }
    public double June { get; set; }
    public double July { get; set; }
    public double Aug { get; set; }
    public double Sep { get; set; }
    public double Oct { get; set; }
    public double Nov { get; set; }
    public double Dec { get; set; }
    public double Total { get; set; }
}

请记住,您的 C# 类应该看起来像您的数据,而 JSON.NET 可以很好地告诉您问题出在哪里。在尝试将您的数据反序列化到您的类中时,我遇到了以下两个错误:

Newtonsoft.Json.JsonReaderException: '无法将字符串转换为 DateTime: H&S 01。路径'[0].Facility',第 1 行,位置 21。'

这使我检查了您的数据和 C# 类中的 Facility 字段。

Newtonsoft.Json.JsonReaderException:'输入字符串'222.0'不是有效整数。路径'[0].Jan',第 1 行,位置 131。'

同样,这导致我检查 JSON 数据和 C# 类中的 Jan-Dec 值。

请注意,您实际上应该在问题中提供此异常信息,以帮助人们帮助您。

【讨论】:

    【解决方案2】:

    只是我们需要改变根类 并在未来使用它。 Json2Cshrap

     public class Root
        {
            public string Facility { get; set; }
            public List<string> Address { get; set; }
            public List<string> City { get; set; }
            public List<string> State { get; set; }
            public List<string> ZipCode { get; set; }
            public List<string> AQDMID { get; set; }
            public double Jan { get; set; }
            public double Feb { get; set; }
            public double March { get; set; }
            public double April { get; set; }
            public double May { get; set; }
            public double June { get; set; }
            public double July { get; set; }
            public double Aug { get; set; }
            public double Sep { get; set; }
            public double Oct { get; set; }
            public double Nov { get; set; }
            public double Dec { get; set; }
            public double Total { get; set; }
        }
    

    【讨论】:

    • 如果我提供的答案解决了您的问题,您应该接受它而不是将代码部分发布为您自己的。
    【解决方案3】:

    如果你不想使用 C# 类,你可以使用动态对象

    例子:

    string thejson = @"{
    ""error_code"": 0,
    ""access_token"": ""*******************"",
    ""expires_in"": 7200
    }";
    
    dynamic data = Json.Decode(thejson);
    string theToken = data.access_token;
    

    您将需要 System.Web.Helpers

    【讨论】:

      【解决方案4】:

      你需要创建一个类似这样的结构。

      public class Data
      {
           public List<Root> Root {get;set;}
      }
      
      public class Root
      {
          public DateTime Facility {get;set;}
          public List<string> Address {get;set;}
          public List<string> City {get;set;}
          public List<string> State {get;set;}
          public List<string> ZipCode {get;set;}
          public List<string> AQDMID {get;set;}
          public int Jan {get;set;}
          public int Feb {get;set;}
          public int March {get;set;}
          public int April {get;set;}
          public int May {get;set;}
          public int June {get;set;}
          public int July {get;set;}
          public int Aug {get;set;}
          public string Sep {get;set;}
          public string Oct {get;set;}
          public string Nov {get;set;}
          public string Dec {get;set;}
          public int Total {get;set;}
      }
      

      之后,您应该可以使用它对其进行反序列化

      List<Root> fetch = Newtonsoft.Json.JsonConvert.DeserializeObject<Data>(json);
      

      【讨论】:

      • 这对 OP 的代码没有任何好处并且不起作用。
      猜你喜欢
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多