2017-02-16 22 views
0

我試圖使用控制檯應用程序來使用我的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 \「}]」

+0

你可以分享我們的JSON數據嗎?這真的有幫助。謝謝! – mindOfAi

+0

嗨,這是json數據「[{\」PayorCode \「:\」STARCAR \「,\」Adminlvl \「:\」1 \「,\」LvlDesc \「:\」Administrator \ 「PayorCode \」:\「STARCAR \」,\「Adminlvl \」:\「2 \」,\「LvlDesc \」:\「System Admin \」},{\「PayorCode \」:\「STARCAR \ \「Adminlvl \」:\「3 \」,\「LvlDesc \」:\「System User \」}]「 – golteb

+0

這是你在這一行中得到的:'HttpResponseMessage res = await cons.GetAsync(」api/PayorPortal/GetPayorUserLevel payorCode = STARCAR&adminlvl = 1" );?'? – mindOfAi

回答

0

當你得到你的JSON數據時,請檢查你是否得到一個或一個列表。您得到這個Newtonsoft.Json.JsonSerializationException的原因是因爲您將它映射到IndividualModel,因此它無法將您的JSON數據映射到您的模型。

我的建議是你在GetAsync()之後加上一個斷點,然後檢查它是否是一個List。如果是的話,改變這一行:

IndividualModel tag = await res.Content.ReadAsAsync<IndividualModel>(); 

這樣:

List<IndividualModel> tag = await res.Content.ReadAsAsync<List<IndividualModel>>(); 

希望它能幫助!

+0

它工作!我的模型得到的值,但標籤的價值是「{ConsoneApplication1.Program.IndividualModel}」,但無論如何,謝謝先生,即時通訊我會自己弄清楚這一點。 :D很好的幫助! – golteb

+0

您現在應該調用每個值,例如tag [0] .etc。這是一個例子: Console.WriteLine(「{0} \ t {1} \ t \ t {2}」,tag [0] .Adminlvl,tag [0] .LvlDesc,tag [0] .PayorCode); – mindOfAi

+0

已經在這裏評論解決方案。 Upvote的答案,菲律賓同胞:P令人敬畏的名字順便說一句。 Hahahaha – mindOfAi