c#不能读取和解释web API/JSON

本文关键字:web API JSON 解释 不能 不能读 读取 | 更新日期: 2023-09-27 18:07:35

我在这里的第一个问题,可能是因为我发现了很多以前的问题&我只是一个业余程序员,我只知道一些基础知识,但我就是喜欢编程下面这个问题我已经头疼了2天了,不知道你们能不能帮我一下?

我正在为我的比特币/山寨币矿工(特别是NVIDIA矿工的ccminer)编程GUI监视器,如果我为它设置了配置,我希望矿工能够跳上最赚钱的硬币。获得这些数据最简单的方法是通过众多的web API,比如这个和这个。所以你可以看到,有许多API(本可以链接更多,但还不允许),但它们似乎都不起作用。

这是我到目前为止的代码:

class Api
{
    public static List<Coins> _download_serialized_json_data(string address)
    {
        List<Coins> coinList = new List<Coins>();
        using (WebClient web = new WebClient())
        {
            string jsonData = web.DownloadString(address);
            JObject json = JObject.Parse(jsonData);

            for (int i = 1; i <= 10; i++)
            {
                Coins c = new Coins();
                c.tag = json["coins"][i]["tag"];
                coinList.Add(c);
            }
        }
        return coinList;
    }
}
public class Coins
{
    public string tag { get; set; }
}

ATM,我使用调试模式只是为了看看对象内部的内容,但是当我试图在这个api(或任何其他具有相应元素的)上使用我的方法时,但是在

c.tag = json["coins"][i]["tag"];

出错了。我不知道在哪里找到确切的错误,但即使当我尝试JArray。解析它只是不起作用。我是不是犯了一个关键的错误?

提前感谢!

c#不能读取和解释web API/JSON

try

 c.tag = json["coins"][i]["tag"].ToString();

你想做这样的事情吗?

Webclient wc = new Webclient();
var json = wc.DownloadString("http://www.whattomine.com/coins.json"); //your 2nd link
var coins = JsonConvert.DeserializeObject<Coins>(json);

public class Coins
{
    public Dictionary<string, Coin> coins = null;
}
public class Coin
{
    public string tag { get; set; }
    public string algorithm { get; set; }
    public double block_reward { get; set; }
    public int block_time { get; set; }
    public int last_block { get; set; }
    public double difficulty { get; set; }
    public double difficulty24 { get; set; }
    public double nethash { get; set; }
    public double exchange_rate { get; set; }
    public string market_cap { get; set; }
    public double volume { get; set; }
    public int profitability { get; set; }
    public int profitability24 { get; set; }
}