获取最新汇率

class Program
  {
      public class ExRate
      {
          public string bank { get; set; }
          public string currency { get; set; }
          public string code { get; set; }
          public decimal? currencyUnit { get; set; }
          public decimal? cenPrice { get; set; }
          public decimal? buyPrice1 { get; set; }
          public decimal? sellPrice1 { get; set; }
          public decimal? buyPrice2 { get; set; }
          public decimal? sellPrice2 { get; set; }
          public DateTime releasedate { get; set; }
          public decimal? Rate { get { return buyPrice1 / (currencyUnit ?? 100); } }
      }

     static void Main(string[] args)
      {
          string url = "http://data.bank.hexun.com/other/cms/foreignexchangejson.ashx";
          WebClient webClient = new WebClient();
          webClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于向Internet资源的请求进行身份验证的网络凭据
          Byte[] pageData = webClient.DownloadData(url); //从指定网站下载数据
          string pageHtml = Encoding.Default.GetString(pageData);  //如果获取网站页面采用的是GB2312,则使用这句
          pageHtml = pageHtml.Replace("ForeignexchangeData(", "").Replace("])", "]");
          List<ExRate> dycs = JsonConvert.DeserializeObject<List<ExRate>>(pageHtml);
          var q =
          from p in dycs
          group p by new { p.code, p.currency } into g
          select new
          {
              g.Key,
              //这里是取几家银行的平均值 我不了解标准的算法是什么 了解的可以自行修改
              AveragePrice = g.Average(p => p.buyPrice1 / (p.currencyUnit ?? 100))
          };

         foreach (var item in q)
          {
              if (!string.IsNullOrEmpty(item.Key.code))
                  Console.WriteLine(item.Key.code + "\t" + item.Key.currency + "\t\t" + item.AveragePrice + "\r\n");
          }
          Console.Read();
      }
  }

posted @   陈恩点  阅读(576)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示