從ASP.NET Core API獲取數(shù)據(jù)

我正在市場(chǎng)和金融新聞工作app.I從https://www.marketaux.com/獲取API。我正在嘗試將網(wǎng)站上的新聞顯示在主頁上。我在ASP.NET Core web應(yīng)用程序文件中創(chuàng)建了一個(gè)模型文件和控制器。在控制器文件中,我在response.data部分得到一個(gè)錯(cuò)誤,因?yàn)樗娘@示響應(yīng)沒有數(shù)據(jù)對(duì)象,而當(dāng)我打印response.Content的輸出時(shí),它有數(shù)據(jù)對(duì)象。你能告訴我如何解決這個(gè)問題,并從API訪問數(shù)據(jù),以便我可以在主頁上顯示它嗎?

Controller class:

using System.Runtime.CompilerServices;
using Azure.Core;
using MarketNews.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;
using MySqlX.XDevAPI;
using Newtonsoft.Json;
using RestSharp;

namespace MarketNews.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class NewsController : ControllerBase
    {
        [HttpGet]
        public News[] GetNews() 
        {
            List<News> news = new List<News>();
            RestClient client = new RestClient("https://api.marketaux.com/v1/news/all");
            // client.Timeout = -1;

            RestRequest request = new RestRequest("News",Method.Get);
            request.AddQueryParameter("api_token", "qIWtsblpK93oeo23o87egUGBoVmVaqkl4fdHRTEc");
            request.AddQueryParameter("symbols", "aapl,amzn");
            request.AddQueryParameter("limit", "50");

            RestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);

            if (response != null)
            {
                foreach (var article in response.data)
                {
                    news.Add(new News
                    {
                        Title = article.title,
                        Description = article.description,
                        Url = article.url,
                        Url_Image = article.image_url,
                        Published_At = article.published_at
                    });
                }
            }

            return news.ToArray();
        }
    }
}

Model class

namespace MarketNews.Models
{
    public class News
    {
        public string Title;
        public string Description;
        public string Url;
        public string Url_Image;
        public DateTime Published_At;
    }
}

我想從API獲取數(shù)據(jù)并將其顯示在主頁上。我使用RestClientRestRequest獲取響應(yīng),然后從響應(yīng)中獲取響應(yīng)。當(dāng)response.Content打印到控制臺(tái)時(shí),我得到了輸出,它正在工作,它給了我一個(gè)json文件作為輸出。在foreach循環(huán)中,當(dāng)我試圖將響應(yīng)數(shù)據(jù)設(shè)置為我創(chuàng)建的模型數(shù)據(jù)時(shí),它顯示response.data不存在。

我想知道這里有什么問題,或者有沒有其他方法可以從API獲取數(shù)據(jù)?

Api:網(wǎng)站鏈接

? 最佳回答:

從RestSharp文檔中,Execute()方法確實(shí)支持泛型類型。

Execute<T>(IRestRequest request)

請(qǐng)注意,此方法已棄用。您應(yīng)尋找以下方法:

ExecuteAsync<T>(IRestRequest request, CancellationToken cancellationToken)

并改進(jìn)API操作以支持異步操作。

從鏈接中共享的響應(yīng)數(shù)據(jù)中,您需要一個(gè)Root對(duì)象,該對(duì)象包含data屬性和List<New>類型。

  1. 定義響應(yīng)的數(shù)據(jù)模型類。
using System.Text.Json;

public class Root
{
    [JsonPropertyName("data")]
    public List<News> Data { get; set; }
}
public class News
{
    [JsonPropertyName("title")]
    public string Title { get; set; }
    
    [JsonPropertyName("description")]
    public string Description { get; set; }

    [JsonPropertyName("url")]
    public string Url { get; set; }

    [JsonPropertyName("image_url")]
    public string Url_Image { get; set; }

    [JsonPropertyName("published_at")]
    public DateTime Published_At { get; set; }
}
  1. 對(duì)于調(diào)用者,將泛型類型指定為Root。接下來用response.Data.Data從響應(yīng)中提取data屬性。
RestResponse<Root> response = client.Execute<Root>(request);

// asynchronous way
// RestResponse<Root> response = await client.ExecuteAsync<Root>(request);

if (response != null)
    news = response.Data.Data;
主站蜘蛛池模板: 无码乱人伦一区二区亚洲| 人妻少妇精品视频一区二区三区| 国产精品一区二区毛卡片| 国精产品一区一区三区免费视频 | 日本精品高清一区二区| 精品无码成人片一区二区| 搡老熟女老女人一区二区| 夜夜嗨AV一区二区三区| 亚洲一区二区精品视频| 国产主播一区二区三区| 国产伦精品一区二区三区不卡| 九九久久99综合一区二区| 中文字幕精品无码一区二区| 国产成人精品一区二三区在线观看 | 国产一区二区三区免费| 国产美女视频一区| 搜日本一区二区三区免费高清视频| 99国产精品欧美一区二区三区| 国产福利一区二区三区在线视频 | 国产成人精品一区二三区在线观看| 无码一区二区三区亚洲人妻| 免费看无码自慰一区二区| 欧美日韩国产免费一区二区三区| 亚洲精品精华液一区二区| 99精品国产高清一区二区| 亚洲av乱码一区二区三区香蕉| 国产爆乳无码一区二区麻豆| 高清国产AV一区二区三区| 久久精品黄AA片一区二区三区| 亚洲日本精品一区二区| 亚洲一区无码中文字幕乱码| 中文无码一区二区不卡αv| 在线电影一区二区| 亚洲第一区精品日韩在线播放| 亚洲美女高清一区二区三区| 日韩免费视频一区二区| 成人精品一区二区户外勾搭野战| 亚洲国产精品无码第一区二区三区| 精品国产免费一区二区| 亚洲国产成人久久一区WWW | 无码一区二区三区免费|