c# - Consuming Api in Console Application -


i'm trying consume api using console application , i'm getting exception. when i'm trying put json data model, these following exceptions:

exception thrown: 'newtonsoft.json.jsonserializationexception' in mscorlib.dll exception thrown: 'system.aggregateexception' in mscorlib.dll exception thrown: 'newtonsoft.json.jsonserializationexception' in consoleapplication1.exe

my source code:

class program {     public class individualmodel     {         public string payorcode { get; set; }         public string adminlvl { get; set; }         public string lvldesc { get; set; }     }      static void main(string[] args)     {         httpclient cons = new httpclient();         cons.baseaddress = new uri("http://localhost:52505/");         cons.defaultrequestheaders.accept.clear();         cons.defaultrequestheaders.accept.add(new system.net.http.headers.mediatypewithqualityheadervalue("application/json"));          try         {             myapiget(cons).wait();         }         catch (aggregateexception e)         {             try             {                 throw e.getbaseexception();             }             catch (exception ea)             {                 //throw ea.innerexception;                 console.writeline(ea.tostring());             }         }     }      static async task myapiget(httpclient cons)     {         using (cons)         {             httpresponsemessage res = await cons.getasync("api/payorportal/getpayoruserlevel?payorcode=starcar&adminlvl=1");             res.ensuresuccessstatuscode();             if (res.issuccessstatuscode)             {                 individualmodel tag = await res.content.readasasync<individualmodel>();                 console.writeline("\n");                 console.writeline("---------------------calling operation------------------------");                 console.writeline("\n");                 console.writeline("tagid tagname tagdescription");                 console.writeline("-----------------------------------------------------------");                 console.writeline("{0}\t{1}\t\t{2}", tag.adminlvl, tag.lvldesc, tag.payorcode);                 console.readline();             }         }     }   } } 

this json data

"[{\"payorcode\":\"starcar\",\"adminlvl\":\"1\",\"lvldesc\":‌​\"administrator\"},{‌​\"payorcode\":\"star‌​car\",\"adminlvl\":\‌​"2\",\"lvldesc\":\"s‌​ystem admin\"},{\"payorcode\":\"starcar\",\"adminlvl\":\"3\",\"lvl‌​desc\":\"system user\"}]"

when you're getting json data, please check if you're getting single 1 or list. reason you're getting newtonsoft.json.jsonserializationexception because can't map json data model since you're mapping individualmodel.

my suggestion add breakpoint right after getasync() check if it's list. if is, change line:

individualmodel tag = await res.content.readasasync<individualmodel>(); 

to this:

list<individualmodel> tag = await res.content.readasasync<list<individualmodel>>(); 

hope helps!


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -