我試圖使用控制檯應用程序來使用我的api並且 我收到異常。當我試圖把我的JSON數據,以我的模型,這些都是下列情況除外:在控制檯應用程序中使用Api
拋出異常:在「System.AggregateException」:在 mscorlib.dll中的異常「Newtonsoft.Json.JsonSerializationException」甩 mscorlib.dll中拋出異常: 'Newtonsoft.Json.JsonSerializationException' 在 ConsoleApplication1.exe中
我的源代碼:
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 Get 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();
}
}
}
}
}
這是JSON數據
「[{\」 PayorCode \ 「:\」 STARCAR \ 「\ 」Adminlvl \「:\ 」1 \「 \ 」LvlDesc \「:\」 Administrator \「},{\」PayorCode \「:\」STAR CAR \「,\」Adminlvl \「:\」2 \「,\」LvlDesc \「:\」系統 Admin \ 「},{\」PayorCode \「:\」STARCAR \「,\」Adminlvl \「:\」3 \「,\」Lvl Desc \「:\」System User \「}]」
你可以分享我們的JSON數據嗎?這真的有幫助。謝謝! – mindOfAi
嗨,這是json數據「[{\」PayorCode \「:\」STARCAR \「,\」Adminlvl \「:\」1 \「,\」LvlDesc \「:\」Administrator \ 「PayorCode \」:\「STARCAR \」,\「Adminlvl \」:\「2 \」,\「LvlDesc \」:\「System Admin \」},{\「PayorCode \」:\「STARCAR \ \「Adminlvl \」:\「3 \」,\「LvlDesc \」:\「System User \」}]「 – golteb
這是你在這一行中得到的:'HttpResponseMessage res = await cons.GetAsync(」api/PayorPortal/GetPayorUserLevel payorCode = STARCAR&adminlvl = 1" );?'? – mindOfAi