在控制台应用程序中使用Api [英] Consuming Api in Console Application

查看:213
本文介绍了在控制台应用程序中使用Api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用控制台应用程序使用我的api,并且 我有一个例外.当我尝试将JSON数据放入模型时,以下是例外情况:

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:

引发的异常:"Newtonsoft.Json.JsonSerializationException"在 mscorlib.dll引发的异常:"System.AggregateException"在 抛出mscorlib.dll异常: 中的'Newtonsoft.Json.JsonSerializationException' ConsoleApplication1.exe

Exception thrown: 'Newtonsoft.Json.JsonSerializationException' in mscorlib.dll Exception thrown: 'System.AggregateException' in mscorlib.dll Exception thrown: 'Newtonsoft.Json.JsonSerializationException' in 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 \:\" Sy ystem Admin \},{\" PayorCode \:\" STARCAR \,\" Adminlvl \:\" 3 \,\" Lvl‌Desc \:\"系统 用户\}]"

"[{\"PayorCode\":\"STARCAR\",\"Adminlvl\":\"1\",\"LvlDesc\":‌​\"Administrator\"},{‌​\"PayorCode\":\"STAR‌​CAR\",\"Adminlvl\":\‌​"2\",\"LvlDesc\":\"S‌​ystem Admin\"},{\"PayorCode\":\"STARCAR\",\"Adminlvl\":\"3\",\"Lvl‌​Desc\":\"System User\"}]"

推荐答案

获取JSON数据时,请检查您是单个还是一个列表.获得此Newtonsoft.Json.JsonSerializationException的原因是因为您无法将JSON数据映射到您的模型,因为您正在将其映射到IndividualModel.

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.

我的建议是您在GetAsync()之后添加一个断点,然后检查它是否为列表.如果是这样,则更改此行:

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>();

对此:

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

希望有帮助!

这篇关于在控制台应用程序中使用Api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆