Json词典使用RestSharp/Newtonsoft.Json对结果进行反序列化. [英] Json dictionary result deserialization with RestSharp/Newtonsoft.Json.

查看:157
本文介绍了Json词典使用RestSharp/Newtonsoft.Json对结果进行反序列化.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用restsharp和newtonsoft.json以便与我的Python API通信.

I am using restsharp and a newtonsoft.json in order to communicate with my Python API.

通讯正常.返回简单字符串的基本查询已得到处理,所以这不是问题.

Communications works fine. Basic queries, which return simple strings are processed, so that's not a problem.

我对反序列化更复杂的结果有疑问,这是我第一次这样做.

I have problem with deserialization of more complex results and i am doing this for the first time.

因此,从Web服务的角度来看,我将返回以下json结构:

So, from web service point of view i am returning following json structure:

{
  "Employee": [
        {
          "Department.DepartmentName": "IT", 
          "Employee.EmployeeArchived": 0, 
          "Employee.EmployeeDepartmentId": 13, 
          "Employee.EmployeeFired": 0, 
          "Employee.EmployeeId": 1, 
          "Employee.EmployeeName": "Name1", 
          "Employee.EmployeePID": 292, 
          "Employee.EmployeeSurname": "Surname1"
    }, 
    {
          "Department.DepartmentName": "IT", 
          "Employee.EmployeeArchived": 0, 
          "Employee.EmployeeDepartmentId": 4, 
          "Employee.EmployeeFired": 0, 
          "Employee.EmployeeId": 2, 
          "Employee.EmployeeName": " Name2", 
          "Employee.EmployeePID": 50, 
          "Employee.EmployeeSurname": " Surname2"
    }
]
}

在我的C#程序中的response.contents(RestClient.Execute的结果)中 我收到了类似的信息(我将其截断为一个数据行):

in response.contents (result of RestClient.Execute) in my C# programm i am getting something like this (i truncated this to just one data row):

{\n  \"Employee\": [\n    {\n      \"Department.DepartmentName\": \"EDV\", \n      \"Employee.EmployeeArchived\": 0, \n      \"Employee.EmployeeDepartmentId\": 13, \n      \"Employee.EmployeeFired\": 0, \n      \"Employee.EmployeeId\": 1, \n      \"Employee.EmployeeName\": \"Name1\", \n      \"Employee.EmployeePID\": 292, \n      \"Employee.EmployeeSurname\": \"Surname1\"\n    }

到目前为止看起来还不错.

So far it looks good.

现在.我试图定义我的数据类以使其能够反序列化,如下所示:

Now. I tried to define my data class to be able to deserialize it as follows:

public class Employee
    {
        [JsonProperty("Department.DepartmentName")]
        public string Department { get; set; }

        [JsonProperty("Employee.EmployeeName")]
        public string Name {get; set;}

        [JsonProperty("Employee.EmployeeSurname")]
        public string Surname { get; set; }

        [JsonProperty("Employee.EmployeeArchived")]
        public int Archived { get; set; }

        [JsonProperty("Employee.EmployeeFired")]
        public int Fired { get; set; }

        [JsonProperty("Employee.EmployeeId")]
        public int Id { get; set; }

        [JsonProperty("Employee.EmployeeDepartmentId")]
        public int DepId { get; set; }

        [JsonProperty("Employee.EmployeePID")]
        public int PID { get; set; }

另外,类包含一个构造函数,但是我认为这不是问题.

In addition class contains a constructor, but i assume that's not the problem.

现在,只需调用:

 var Data = JsonConvert.DeserializeObject<Employee>(response.Content);

...返回空值.我想问题是我返回了一个字典"Employee",而不仅仅是一个数据行.我需要如何设计才能正确反序列化此方法?

... returns a null. I suppose the problem is i return a dictionary "Employee", not just the single data row. How i would need to design it to deserialize this correctly?

推荐答案

看起来它比我想象的要简单. 我将"Employee"类重命名为"EmployeeData"(仅出于优雅和与json参数名称兼容的目的).

Looks like it was simpler than i thought. I renamed "Employee" class to "EmployeeData" (just for sake of elegancy & compatibility with json parameter names).

然后我定义了第二类:

class EmployeeList
    {
        [JsonProperty("Employee")]
        public List<EmployeeData> Employee { get; set; }
    }

然后

JsonConvert.DeserializeObject<EmployeeList>(response.Content);

返回正确填充的班级,其中包含员工列表. 不重要的. Json.net确实功能强大.

returns correctly filled class with list of employees. Trivial. Json.net is really powerfull.

这篇关于Json词典使用RestSharp/Newtonsoft.Json对结果进行反序列化.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆