using System; using System.Net.Http; using System.Threading.Tasks; using Newtonsoft.Json.Linq; class Exchange { private const string AppKey = "YOUR_APPKEY_HERE"; // 替换为你的真实 appkey // 查询银行汇率信息 public static async Task BankAsync(string bank) { try { using var client = new HttpClient(); string url = $"https://api.jisuapi.com/exchange/bank?appkey={AppKey}&bank={bank}"; HttpResponseMessage response = await client.GetAsync(url); response.EnsureSuccessStatusCode(); string result = await response.Content.ReadAsStringAsync(); JObject jsonarr = JObject.Parse(result); if ((int)jsonarr["status"] != 0) { Console.WriteLine($"API返回错误: {jsonarr["status"]}-{jsonarr["msg"]}"); return; } Console.WriteLine($"银行代号:{bank}"); // 从 jsonarr 中获取 result 对象,使用空合并运算符处理可能的 null 值 JObject? resultObj = jsonarr["result"] as JObject; if (resultObj == null) { Console.WriteLine("未找到有效的结果数据"); return; } // 处理 list 部分,判断是 JObject 还是 JArray var listToken = resultObj["list"]; if (listToken is JObject listObj) { foreach (var item in listObj) { if (item.Value is JObject value) { foreach (var subItem in value) { // 简化内插字符串 Console.WriteLine($"{subItem.Key.PadRight(8)}: {subItem.Value}"); } Console.WriteLine("----------------------------------"); } } } else if (listToken is JArray listArray) { foreach (var item in listArray) { if (item is JObject value) { foreach (var subItem in value) { // 简化内插字符串 Console.WriteLine($"{subItem.Key.PadRight(8)}: {subItem.Value}"); } Console.WriteLine("----------------------------------"); } } } else { Console.WriteLine("未找到有效的列表数据"); } } catch (HttpRequestException ex) { Console.WriteLine($"请求出错: {ex.Message}"); } catch (Exception ex) { Console.WriteLine($"发生错误: {ex.Message}"); } } static async Task Main() { // 调用货币转换方法 await Exchange.ConvertAsync("CNY", "USD", 10); // 调用查询单一货币信息方法 await Exchange.SingleAsync("CNY"); // 调用查询所有货币信息方法 await Exchange.CurrencyAsync(); // 十大银行外汇牌价 await Exchange.BankAsync("BOC"); } }
© 2015-2025 杭州极速互联科技有限公司 版权所有 浙ICP备17047587号-4 浙公网安备33010502005096 增值电信业务经营许可证:浙B2-20190875