【问题标题】:Querying WebAPI - webapi Cannot deserialize the current JSON array查询 WebAPI - webapi 无法反序列化当前 JSON 数组
【发布时间】:2019-12-13 05:18:46
【问题描述】:

我使用 MVC 构建了一个 Web API,它按预期工作。

我现在尝试从控制台应用程序查询 API,但遇到了问题。我明白我为什么会遇到这个问题,但我不明白如何解决它。

我的控制台应用程序代码:

static HttpClient client = new HttpClient();
        static  void Main(string[] args)
        {

            RunAsync().GetAwaiter().GetResult();

        }

        static async Task RunAsync()
        {

            client.BaseAddress = new Uri(URL);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

            List<TagDetail> tagDetail = new List<TagDetail>();

            tagDetail = await GetTagDetailAsync("api/tagdetail/?tagname=myTag&startdate=010120190000&enddate=020120190000");
            Console.WriteLine(tagDetail.value);
        }

            static async Task<TagDetail> GetTagDetailAsync(string path)
        {
            List<TagDetail> tagdetail = new List<TagDetail>();
            HttpResponseMessage response = await client.GetAsync(path);
            var test = response.StatusCode;
            var test2 = response.Headers;
            if (response.IsSuccessStatusCode)
            {
                tagdetail = await response.Content.ReadAsAsync<List<TagDetail>>(
            new List<MediaTypeFormatter>
            {
                new XmlMediaTypeFormatter(),
                new JsonMediaTypeFormatter()
            }); 
            }
            return tagdetail;
        }

我得到的错误是:

tagDetail = await GetTagDetailAsync("api/tagdetail/?tagname=99TOTMW&startdate=010120190000&enddate=020120190000");

return tagdetail;

Web API 以 JSON 格式返回数据,如下所示:

{  
   "tagname":"myTag",
   "value":"99.99",
   "description":"myDescription",
   "units":"£",
   "quality":"Good",
   "timestamp":"2019-08-01T17:32:30"
},
{  
   "tagname":"myTag",
   "value":"22.22",
   "description":"myDescription",
   "units":"£",
   "quality":"Good",
   "timestamp":"2019-08-01T17:33:30"
}

TagDetail 类只是您在上面看到的每个字段的声明。

webapi 提供了选择日期范围的方法,因此我可以将大量 TagDetails 作为列表返回,但它也可以只返回一个(我可以通过稍微更改我的代码来使其工作)。我需要它来处理一个或多个结果。

【问题讨论】:

  • 你能不能把你的代码改成这个,看看你的错误是否消失了,static async Task&lt;IEnumerable&lt;TagDetail&gt;&gt; GetTagDetailAsync(string path)。您的 Web API 提供单个 TagDetail 对象,但您正在传回 List。处理客户端返回的记录数。另外,您希望tagDetail.value 是什么?

标签: c# asp.net-core asp.net-web-api2


【解决方案1】:

正如评论所解释的,您需要为您的GetTagDetailAsync 返回List&lt;TagDetail&gt;。然后您可以使用foreach 循环结果。这将适用于一个或多个TagDetail

static async Task RunAsync()
    {

        //other logic
        List<TagDetail> tagDetail = new List<TagDetail>();

        tagDetail = await GetTagDetailAsync("api/tagdetail/?tagname=myTag&startdate=010120190000&enddate=020120190000");

        foreach(var item in tagDetail)
        {
            Console.WriteLine(item.value);
        }
    }

        static async Task<List<TagDetail>> GetTagDetailAsync(string path)
    {
        List<TagDetail> tagdetail = new List<TagDetail>();
        HttpResponseMessage response = await client.GetAsync(path);
        var test = response.StatusCode;
        var test2 = response.Headers;
        if (response.IsSuccessStatusCode)
        {
            tagdetail = await response.Content.ReadAsAsync<List<TagDetail>>(
        new List<MediaTypeFormatter>
        {
            new XmlMediaTypeFormatter(),
            new JsonMediaTypeFormatter()
        }); 
        }
        return tagdetail;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多