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

How to deserialize JSON to .net objects

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

这是我的代码:

var client = newRestClient("https:xxxxxxxxxxxxxxxxxx/pincodes/");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\xxxxxxxxxxxxxxxx\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"password\"\r\n\r\xxxxxxxxxxxxxxxx\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
string JsonContent = response.Content;

这是 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"}]

我想访问特定值,例如。 city, pincodes 等的值 如何创建模型,我试过了,但出现了一些 错误:"Error CS0825 The contextual keyword 'var' may only appear within a local variable declaration or in script code"

最好的方法是使用Json.NET

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name; // Bad Boys

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

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

这会给你一个包含你的数据的列表

输出