Json数据转换为C#类

Json数据转换为C#类,c#,json,asp.net-mvc,C#,Json,Asp.net Mvc,我需要反序列化以下JSON: [ { "Facility": "H&S 01", "Address": [ "5999 Cerritos Ave." ], "City": [ "anaheim" ], "State": [ "ca" ], "ZipCode": [ 92831 ], "AQDMID": [ 1 ], "Jan": 222.0, "F

我需要反序列化以下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
      }
    ]
我曾经创建类来表示我的数据,并得到以下结果:

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;}
}
公共类根目录
{
公共日期时间设施{get;set;}
公共列表地址{get;set;}
公共列表城市{get;set;}
公共列表状态{get;set;}
公共列表ZipCode{get;set;}
公共列表AQDMID{get;set;}
公共整数Jan{get;set;}
公共int Feb{get;set;}
公共整数{get;set;}
公共int April{get;set;}
公共int可以{get;set;}
public int June{get;set;}
public int July{get;set;}
public int Aug{get;set;}
公共字符串Sep{get;set;}
公共字符串Oct{get;set;}
公共字符串Nov{get;set;}
公共字符串Dec{get;set;}
公共整数总计{get;set;}
}
我正在尝试使用JSON.NET反序列化我的数据:

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

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

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

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;}
}
公共类数据
{
公共列表根{get;set;}
}
公共类根
{
公共日期时间设施{get;set;}
公共列表地址{get;set;}
公共列表城市{get;set;}
公共列表状态{get;set;}
公共列表ZipCode{get;set;}
公共列表AQDMID{get;set;}
公共整数Jan{get;set;}
公共int Feb{get;set;}
公共整数{get;set;}
公共int April{get;set;}
公共int可以{get;set;}
public int June{get;set;}
public int July{get;set;}
public int Aug{get;set;}
公共字符串Sep{get;set;}
公共字符串Oct{get;set;}
公共字符串Nov{get;set;}
公共字符串Dec{get;set;}
公共整数总计{get;set;}
}
之后,您应该能够使用

List<Root> fetch = Newtonsoft.Json.JsonConvert.DeserializeObject<Data>(json);
List fetch=Newtonsoft.Json.JsonConvert.DeserializeObject(Json);
我建议使用将数据转换为C类,或者使用Visual Studio中的
编辑|粘贴特殊|粘贴为JSON类
菜单选项。您使用的服务似乎不能很好地猜测字段代表什么

您的类与您的JSON不匹配:

"Facility": "H&S 01",
"Facility": "H&S 02",
这些不是模型中声明的
DateTime
对象:
publicdatetime工具{get;set;}

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

固定类可能如下所示:

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; }
}
公共类根目录
{
公共字符串工具{get;set;}
公共列表地址{get;set;}
公共列表城市{get;set;}
公共列表状态{get;set;}
公共列表ZipCode{get;set;}
公共列表AQDMID{get;set;}
公共双Jan{get;set;}
公共双Feb{get;set;}
公共双三月{get;set;}
公共双四月{get;set;}
公共双精度可{get;set;}
公共双六月{get;set;}
公共双七月{get;set;}
公共双Aug{get;set;}
公共双Sep{get;set;}
公共双Oct{get;set;}
公共双Nov{get;set;}
公共双Dec{get;set;}
公共双总计{get;set;}
}
请记住,您的C#类应该看起来像您的数据,JSON.NET可以很好地告诉您问题的确切位置。在尝试将数据反序列化到类中时,我遇到以下两个错误:

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

这让我检查了数据和C#class中的
Facility
字段

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

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


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

只是我们需要更改根类,如 并在将来使用它。

公共类根目录
{
公共字符串工具{get;set;}
公共列表地址{get;set;}
公共列表城市{get;set;}
公共列表状态{get;set;}
公共列表ZipCode{get;set;}
公共列表AQDMID{get;set;}
公共双Jan{get;set;}
公共双Feb{get;set;}
公共双三月{get;set;}
公共双四月{get;set;}
公共双精度可{get;set;}
公共双六月{get;set;}
公共双七月{get;set;}
公共双Aug{get;set;}
公共双Sep{get;set;}
公共双Oct{get;set;}
公共双Nov{get;set;}
公共双Dec{get;set;}
公共双总计{get;set;}
}

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

例如:

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

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

你需要System.Web.Helpers

所以你想去C#Class Jason那门课是从哪里来的?这似乎是完全错误的-例如,Facility是一个字符串,而不是DateTime。VisualStudio将为您个人创建这个类,我坚持编写自己的类来保存我必须与之交互的任何JSON数据。它让我确保类与JSON描述的内容相匹配。Facility是如何成为DateTime的?这与OP的代码相比没有任何好处,也不起作用。如果我提供的答案解决了您的问题,您应该接受它,而不是将代码部分作为自己的部分发布。
string thejson = @"{
""error_code"": 0,
""access_token"": ""*******************"",
""expires_in"": 7200
}";

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