简体   繁体   English

Json 数据转换为 C# 类

[英]Json data Convert to C# Class

I have the following JSON which I need to deserialize:我有以下 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
      }
    ]

I used http://jsontotable.com/ to create classes to represent my data and got the following:我使用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;}
}

I'm trying to deserialize my data using JSON.NET:我正在尝试使用 JSON.NET 反序列化我的数据:

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

But it doesn't deserialize to my C# class.但它不会反序列化到我的 C# 类。

I recommend using json2cshap to convert data to C# classes, or the Edit | Paste Special | Paste as JSON classes我建议使用json2cshap将数据转换为 C# 类,或者使用Edit | Paste Special | Paste as JSON classes Edit | Paste Special | Paste as JSON classes Edit | Paste Special | Paste as JSON classes menu option in Visual Studio.在 Visual Studio 中Edit | Paste Special | Paste as JSON classes菜单选项。 It seems that the service you use doesn't do a very good job of guessing what the fields represent.您使用的服务似乎并没有很好地猜测字段代表什么。

Your class doesn't match your JSON:您的课程与您的 JSON 不匹配:

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

These are not DateTime objects as declared in your model: public DateTime Facility {get;set;}这些不是模型中声明的DateTime对象: public DateTime Facility {get;set;}

I'm a bit confused by your int and string Jan-Dec values, and your Total value.我对您的intstring Jan-Dec 值以及您的 Total 值感到有些困惑。 All of the values in your JSON are decimal, but in your C# model they're represented as integers and strings. JSON 中的所有值都是十进制的,但在 C# 模型中,它们表示为整数和字符串。 I recommend using double or decimal for all of these.我建议对所有这些使用doubledecimal

A fixed class might look like this:一个固定的类可能如下所示:

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; }
}

Remember that your C# class should look like your data, and JSON.NET does a good job of telling you exactly where the problem lies.请记住,您的 C# 类应该看起来像您的数据,并且 JSON.NET 可以很好地告诉您问题出在哪里。 In attempting to deserialize your data into your class I got these two errors:在尝试将您的数据反序列化到您的类中时,我遇到了以下两个错误:

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

This led me to check the Facility field in your data and C# class.这让我检查了数据和 C# 类中的Facility字段。

Newtonsoft.Json.JsonReaderException: 'Input string '222.0' is not a valid integer. Newtonsoft.Json.JsonReaderException: '输入字符串 '222.0' 不是有效整数。 Path '[0].Jan', line 1, position 131.'路径 '[0].Jan',第 1 行,位置 131。'

Likewise, this led me to check the Jan-Dec values in your JSON data and C# class.同样,这让我检查了您的 JSON 数据和 C# 类中的 Jan-Dec 值。

Note that you should actually provide this exception information in your question to assist people in helping you.请注意,您实际上应该在问题中提供此异常信息以帮助人们帮助您。

Just we need to change Root Class like and used it future .只是我们需要改变 Root Class 并在未来使用它。 Json2Cshrap 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; }
    }

If you don't want to use a C# class, you could use a dynamic object如果不想使用 C# 类,可以使用动态对象

Example:例子:

string thejson = @"{
""error_code"": 0,
""access_token"": ""*******************"",
""expires_in"": 7200
}";

dynamic data = Json.Decode(thejson);
string theToken = data.access_token;

You will need System.Web.Helpers您将需要 System.Web.Helpers

You need to create a structure something like this.您需要创建一个类似这样的结构。

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;}
}

Afterwards you should be able to deserilize it with之后你应该可以用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM