Consuming Api in Console Application

99封情书 提交于 2019-12-12 03:56:50

问题


I'm trying to consume my api using a console application and I'm getting an exception. When I'm trying to put my JSON data to my model, these are the 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 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();
            }
        }
    }
  }
}

this is the 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\"}]"


回答1:


When you're getting your JSON data, please check if you're getting a single one or a list. The reason you're getting this Newtonsoft.Json.JsonSerializationException is because your it can't map your JSON data to your Model since you're mapping it to an IndividualModel.

My suggestion is you add a breakpoint right after the GetAsync() then check if it's a List. If it is, then change this line:

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

to this:

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

Hope it helps!



来源:https://stackoverflow.com/questions/42264345/consuming-api-in-console-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!