如何反序列化jsonconvert newtonsoft? [英] How to deserialize jsonconvert newtonsoft?

查看:123
本文介绍了如何反序列化jsonconvert newtonsoft?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,您能帮我吗?我有下面的课程

Hello guys Could you help me? I have the class below

public class EmpTraining
{
    public int CodColig { get; set; }
    public string EmpID { get; set; }
    public string Employee { get; set; }
    public string CostCenter { get; set; }
    public string Department { get; set; }
    public string WorkstationID { get; set; }
    public string Workstation { get; set; }
    public string Training { get; set; }
    public string CreateDt { get; set; }
    public string DueDt { get; set; }
}

我需要反序列化下面的json.

And I need to deserialize the json below.

{
  "EmployeesTrainings": [
    {
      "CODCOLIGADA": 1,
      "CHAPA": "sample string 2",
      "NOME": "sample string 3",
      "CCUSTO": "sample string 4",
      "Departamento": "sample string 5",
      "CODPOSTO": "sample string 6",
      "POSTO": "sample string 7",
      "TREINAMENTO": "sample string 8",
      "REALIZADO_EM": "2016-04-19T14:17:19.7291778-03:00",
      "VALIDADE": "2016-04-19T14:17:19.7291778-03:00"
    },
    {
      "CODCOLIGADA": 1,
      "CHAPA": "sample string 2",
      "NOME": "sample string 3",
      "CCUSTO": "sample string 4",
      "Departamento": "sample string 5",
      "CODPOSTO": "sample string 6",
      "POSTO": "sample string 7",
      "TREINAMENTO": "sample string 8",
      "REALIZADO_EM": "2016-04-19T14:17:19.7291778-03:00",
      "VALIDADE": "2016-04-19T14:17:19.7291778-03:00"
    }
  ],
  "HasErrors": true,
  "Errors": [
    "sample string 1",
    "sample string 2"
  ]
}

即使更改错误名称,该错误也会不断发生

Even changing the name of the variables the error keep happening

无法反序列化当前JSON对象(例如{"name":"value"}) 成类型 'System.Collections.Generic.List`1 [Foxconn.Portal.Model.HR.Training.EmpTraining]' 因为该类型需要JSON数组(例如[1,2,3])进行反序列化 正确.

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Foxconn.Portal.Model.HR.Training.EmpTraining]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

要解决此错误,请将JSON更改为JSON数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的.NET 类型(例如,不是整数之类的原始类型,不是集合类型 (例如数组或列表),可以从JSON对象反序列化. 也可以将JsonObjectAttribute添加到类型中以强制将其 从JSON对象反序列化.

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

路径'EmployeesTrainings',第1行,位置22.

Path 'EmployeesTrainings', line 1, position 22.

调用该类进行反序列化的代码是:

The code that is calling the class to deserialize is:

public static List<EmpTraining> List(int codColig, string empID, string trainingID,  string workstationID, string status, int? days)
  {


      List<EmpTraining> empList = Api.PostWebApiStr<List<EmpTraining>>(JSON, webApi, Token, timeOut);

      if (empList.Count > 0)
          return empList;

      return new List<EmpTraining>();

  }

我要反序列化的类是下面的那个.

the class that I'm using to deserialize is that one below.

 public static T PostWebApiStr<T>(string data, Uri webApiUrl, Token token, int timeout)
        {

            using (var client = new ExtendedWebClient(webApiUrl, timeout))
            {
                try
                {

                    client.Headers[HttpRequestHeader.ContentType] = "application/json";

                    if (token != null)
                    {
                        client.Headers[HttpRequestHeader.Authorization] = string.Format("{0} {1}", token.TokenType, token.AccessToken);
                    }

                    var response = client.UploadString(webApiUrl, data);

                    return JsonConvert.DeserializeObject<T>(response);

                }
                catch (WebException ex)
                {

                    throw new Exception(ex.Message);
                }
            }
        }

推荐答案

您可能需要使用下面的类才能将json数据转换为类对象

Hi you probably need to use the below class in order to convert your json data to class object

public class EmployeesTraining
{
    public int CODCOLIGADA { get; set; }
    public string CHAPA { get; set; }
    public string NOME { get; set; }
    public string CCUSTO { get; set; }
    public string Departamento { get; set; }
    public string CODPOSTO { get; set; }
    public string POSTO { get; set; }
    public string TREINAMENTO { get; set; }
    public string REALIZADO_EM { get; set; }
    public string VALIDADE { get; set; }
}

public class RootObject
{
    public List<EmployeesTraining> EmployeesTrainings { get; set; }
    public bool HasErrors { get; set; }
    public List<string> Errors { get; set; }
}

然后您可以浏览EmployeeTrainings列表以获取各个员工的详细信息

You could then look through EmployeeTrainings list for accesing individual employee details

使用以下链接将json数据转换为C#类 http://json2csharp.com/

use the below link to convert your json data to C# classes http://json2csharp.com/

这篇关于如何反序列化jsonconvert newtonsoft?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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