我正在尝试从ETH
到CZK
创建货币转换器,我正在使用API来获取帐户余额和CZK
的实际汇率。
我正在使用它:
WebClient client = new WebClient();
string downloadString = client.DownloadString("https://api.nanopool.org/v1/eth/balance/0x1b0cab6db1672349b8f8a6d8d8903ab58ae0d734");
//Console.WriteLine(downloadString);
downloadString = downloadString.Replace('.', ',');
//Console.WriteLine(downloadString);
string[] first = downloadString.Split(':');
string ETH = first[2].Remove(first[2].Length-1);
Console.WriteLine(ETH);
string downloadString2 = client.DownloadString("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=CZK");
downloadString2 = downloadString2.Replace('.', ',');
string[] second = downloadString2.Split(':');
string CZK = second[1].Remove(second[1].Length - 1);
Console.WriteLine(CZK);
float eth = float.Parse(ETH);
float czk = float.Parse(CZK);
Console.WriteLine("You have {0} CZK", eth * czk);
有没有更好的方法从API删除其他所有内容然后编号?
感谢
答案 0 :(得分:3)
它返回一个JSON。您可以使用JSON解析器解析它。
答案 1 :(得分:3)
答案 2 :(得分:0)
卢卡斯。
为什么不转换为对象? 我想你可以创建一个类。 在这里:
public class Link1Data
{
public bool status { get; set; }
public float data { get; set; }
}
public class Link2CZK
{
public float CZK { get; set; }
}
然后将您的代码更改为:
WebClient webClient = new WebClient();
var data = webClient.DownloadString("https://api.nanopool.org/v1/eth/balance/0x1b0cab6db1672349b8f8a6d8d8903ab58ae0d734");
var ethData = JsonConvert.DeserializeObject<Link1Data>(data);
var data2 = webClient.DownloadString("https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=CZK");
var czkData = JsonConvert.DeserializeObject<Link2CZK>(data2);
Console.WriteLine("You have {0} CZK", ethData.data * czkData.CZK);
基于: How to convert a Stream into an object
我没有测试。 ; - )