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

时间:2019-01-27 08:11:14

标签: c# json json-deserialization

您好,我是 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等的值 我尝试了如何创建模型,但遇到了一些错误:“错误CS0825 上下文关键字'var'只能出现在局部变量声明中或脚本代码中”

4 个答案:

答案 0 :(得分:2)

最好的方法是使用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

答案 1 :(得分:2)

您可以使用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);

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

输出 enter image description here

答案 2 :(得分:1)

您可以具有“城市模型”,然后反序列化为该模型。

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

ListOfCity

否则,您可以使用动态的,但这很昂贵。

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

答案 3 :(得分:1)

最好的选择是使用动态c#类型 ...,它很有帮助

  • 首先,您需要将此内容添加到您的软件包中以获取访问权限 JsonConvert.DeserializeObject

    enter image description here

下面的 JSON字符串是来自HTTP API调用的简单响应,它定义了两个属性:Id和Name

json:

string 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"}]";

并使用:

进行转换
JsonConvert.DeserializeObject<dynamic>()

要将此字符串反序列化为动态类型,然后只需访问其属性usnig jsonDeserialized.atribute ,就像ID和名称中的代码一样:

dynamic res = JsonConvert.DeserializeObject<dynamic>(json);
var city = res[0].city;
var state = res[0].state;

如果将 res变量的类型指定为 dynamic ,而不是使用 var 关键字,则属性值将正确反序列化,例如标识为int而不是JValue

希望有帮助!