如何反序列化 jsonconvert newtonsoft?
How to deserialize jsonconvert newtonsoft?
大家好,能帮帮我吗?我下面有class
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。
{
"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"
]
}
即使更改变量的名称,错误也会继续发生
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.
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.
Path 'EmployeesTrainings', line 1, position 22.
调用 class 进行反序列化的代码是:
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>();
}
我用来反序列化的 class 是下面那个。
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);
}
}
}
您好,您可能需要使用下面的 class 才能将 json 数据转换为 class 对象
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 列表以获取个别员工的详细信息
使用下面的 link 将您的 json 数据转换为 C# classes http://json2csharp.com/
您可以使用attributes更改序列化字段的名称:
public class EmpTraining
{
[JsonProperty('CODCOLIGADA')]
public int CodColig { get; set; }
[JsonProperty('CHAPA')]
public string EmpID { get; set; }
[JsonProperty('NOME')]
public string Employee { get; set; }
[JsonProperty('CCUSTO')]
public string CostCenter { get; set; }
[Jsonproperty('Departamento')]
public string Department { get; set; }
[JsonProperty('CODPOSTO']
public string WorkstationID { get; set; }
[JsonProperty('POSTO')]
public string Workstation { get; set; }
[JsonProperty('TREINAMENTO')]
public string Training { get; set; }
[JsonProperty('REALIZADO_EM')]
public string CreateDt { get; set; }
[JsonProperty('VALIDADE')]
public string DueDt { get; set; }
}
但是您提供的错误信息没有正确解释您的问题。它说:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[CoderwallDotNet.Api.Models.Account]'
所以周围好像还有一个问题,无法解答。请提供完整的代码示例,否则只是猜测。
大家好,能帮帮我吗?我下面有class
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。
{
"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"
]
}
即使更改变量的名称,错误也会继续发生
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.
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.
Path 'EmployeesTrainings', line 1, position 22.
调用 class 进行反序列化的代码是:
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>();
}
我用来反序列化的 class 是下面那个。
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);
}
}
}
您好,您可能需要使用下面的 class 才能将 json 数据转换为 class 对象
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 列表以获取个别员工的详细信息
使用下面的 link 将您的 json 数据转换为 C# classes http://json2csharp.com/
您可以使用attributes更改序列化字段的名称:
public class EmpTraining
{
[JsonProperty('CODCOLIGADA')]
public int CodColig { get; set; }
[JsonProperty('CHAPA')]
public string EmpID { get; set; }
[JsonProperty('NOME')]
public string Employee { get; set; }
[JsonProperty('CCUSTO')]
public string CostCenter { get; set; }
[Jsonproperty('Departamento')]
public string Department { get; set; }
[JsonProperty('CODPOSTO']
public string WorkstationID { get; set; }
[JsonProperty('POSTO')]
public string Workstation { get; set; }
[JsonProperty('TREINAMENTO')]
public string Training { get; set; }
[JsonProperty('REALIZADO_EM')]
public string CreateDt { get; set; }
[JsonProperty('VALIDADE')]
public string DueDt { get; set; }
}
但是您提供的错误信息没有正确解释您的问题。它说:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[CoderwallDotNet.Api.Models.Account]'
所以周围好像还有一个问题,无法解答。请提供完整的代码示例,否则只是猜测。