简体   繁体   中英

Consuming Api in Console Application

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM