C# 如何将JSON反序列化为.net对象

C# 如何将JSON反序列化为.net对象,c#,json,json-deserialization,C#,Json,Json Deserialization,大家好,我是JSON反序列化新手。这是必须反序列化为.net对象的JSON数据,以便我可以将JSON中的值存储在数据库中 这是我的代码: 以下是json文件的外观: [{"city": "AMBALA", "state": "Haryana", "city_type": "", "active": true, "route": "HR/I1H/ABA", "date_of_discontinuance": "", "state_code": "HR", "pincode": 134003, "c

大家好,我是JSON反序列化新手。这是必须反序列化为.net对象的JSON数据,以便我可以将JSON中的值存储在数据库中

这是我的代码:

以下是json文件的外观:

[{"city": "AMBALA", "state": "Haryana", "city_type": "", "active": true, "route": "HR/I1H/ABA", "date_of_discontinuance": "", "state_code": "HR", "pincode": 134003, "city_code": "ABA", "dccode": "ABA"}, 

{"city": "AMBALA", "state": "Haryana", "city_type": "", "active": true, "route": "HR/I1H/ABA", "date_of_discontinuance": "", "state_code": "HR", "pincode": 134002, "city_code": "ABA", "dccode": "ABA"}]
我想访问特定值,例如城市值、节点等。 如何创建模型,我尝试过,但遇到了一些错误:错误CS0825上下文关键字“var”可能只出现在局部变量声明或脚本代码中,最好的方法是使用

最好的方法是使用


您可以拥有一个城市模型,然后反序列化到该模型

 public class CityModel
    {
        public string city { get; set; }
        public string state { get; set; }
        public string city_type { get; set; }
        public bool active { get; set; }
        public string route { get; set; }
        public string date_of_discontinuance { get; set; }
        public string state_code { get; set; }
        public int pincode { get; set; }
        public string city_code { get; set; }
        public string dccode { get; set; }
    }


string JsonResult = @"[{'city': 'AMBALA', state: 'Haryana', 'city_type': '', 'active': true, 'route': 'HR / I1H / ABA', 'date_of_discontinuance': '', 'state_code': 'HR', 'pincode': 134003, 'city_code': 'ABA', 'dccode': 'ABA'},{ 'city': 'AMBALA', 'state': 'Haryana', 'city_type': '', 'active': true, 'route': 'HR/I1H/ABA', 'date_of_discontinuance': '', 'state_code': 'HR', 'pincode': 134002, 'city_code': 'ABA', 'dccode': 'ABA'}]";

var result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CityModel>>(JsonResult);  
除此之外,您还可以使用dynamic,但这是非常昂贵的

List<dynamic> result = JsonConvert.DeserializeObject<List<dynamic>>(JsonResult);
var city = result[0].city;

您可以拥有一个城市模型,然后反序列化到该模型

 public class CityModel
    {
        public string city { get; set; }
        public string state { get; set; }
        public string city_type { get; set; }
        public bool active { get; set; }
        public string route { get; set; }
        public string date_of_discontinuance { get; set; }
        public string state_code { get; set; }
        public int pincode { get; set; }
        public string city_code { get; set; }
        public string dccode { get; set; }
    }


string JsonResult = @"[{'city': 'AMBALA', state: 'Haryana', 'city_type': '', 'active': true, 'route': 'HR / I1H / ABA', 'date_of_discontinuance': '', 'state_code': 'HR', 'pincode': 134003, 'city_code': 'ABA', 'dccode': 'ABA'},{ 'city': 'AMBALA', 'state': 'Haryana', 'city_type': '', 'active': true, 'route': 'HR/I1H/ABA', 'date_of_discontinuance': '', 'state_code': 'HR', 'pincode': 134002, 'city_code': 'ABA', 'dccode': 'ABA'}]";

var result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CityModel>>(JsonResult);  
除此之外,您还可以使用dynamic,但这是非常昂贵的

List<dynamic> result = JsonConvert.DeserializeObject<List<dynamic>>(JsonResult);
var city = result[0].city;

您可以使用Json.Net进行反序列化。第一步是为你的城市定义一个模型

比如说,

public class CityDetail
{
    public string city { get; set; }
    public string state { get; set; }
    public string city_type { get; set; }
    public bool active { get; set; }
    public string route { get; set; }
    public string date_of_discontinuance { get; set; }
    public string state_code { get; set; }
    public int pincode { get; set; }
    public string city_code { get; set; }
    public string dccode { get; set; }
}
现在,您可以使用Json.Net反序列化数据,如下所示

var result = JsonConvert.DeserializeObject<List<CityDetail>>(jsonString);
这将为您提供一个包含数据的列表

输出
您可以使用Json.Net进行反序列化。第一步是为你的城市定义一个模型

比如说,

public class CityDetail
{
    public string city { get; set; }
    public string state { get; set; }
    public string city_type { get; set; }
    public bool active { get; set; }
    public string route { get; set; }
    public string date_of_discontinuance { get; set; }
    public string state_code { get; set; }
    public int pincode { get; set; }
    public string city_code { get; set; }
    public string dccode { get; set; }
}
现在,您可以使用Json.Net反序列化数据,如下所示

var result = JsonConvert.DeserializeObject<List<CityDetail>>(jsonString);
这将为您提供一个包含数据的列表

输出

可能重复的可能重复的但我无法创建模型我对这些概念不熟悉我喜欢您使用模型的结果,但对于初学者,我们应该尝试动态,如我的建议中所述:D@Nadir,我们应该仅在不知道返回的对象类型的情况下使用dynamic。如果我们确定返回的列表类型,我们应该始终使用模型方法。使用dynamic被认为是不好的做法,因为它指的是类型后期绑定,这意味着系统将仅在执行期间而不是在编译期间检查类型。但我无法创建模型我对这些概念不熟悉我喜欢你使用模型的结果,但对于初学者,我们应该尝试动态,正如我在提案中提到的:D@Nadir,我们应该仅在不知道返回的对象类型的情况下使用dynamic。如果我们确定返回的列表类型,我们应该始终使用模型方法。使用dynamic被认为是不好的做法,因为它指的是类型后期绑定,这意味着系统将仅在执行期间而不是在编译期间检查类型。