c# - How to deserialize jsonconvert newtonsoft? -


hello guys me? have 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; } } 

and need deserialize 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 name of variables error keep happening

cannot deserialize current json object (e.g. {"name":"value"}) type 'system.collections.generic.list`1[foxconn.portal.model.hr.training.emptraining]' because type requires json array (e.g. [1,2,3]) deserialize correctly.

to fix error either change json json array (e.g. [1,2,3]) or change deserialized type normal .net type (e.g. not primitive type integer, not collection type array or list) can deserialized json object. jsonobjectattribute can added type force deserialize json object.

path 'employeestrainings', line 1, position 22.

the code calling class 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 i'm using deserialize 1 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);                 }             }         } 

hi need use below class in order convert json data 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; } } 

you through employeetrainings list accesing individual employee details


use below link convert json data c# classes http://json2csharp.com/


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -