【问题标题】:C# can't read and interpret web API/JSONC# 无法读取和解释 Web API/JSON
【发布时间】:2014-07-28 02:12:45
【问题描述】:

我在这里的第一个问题,可能是因为我在这里找到了大量以前的问题和答案:) 我只是一个业余程序员,我只知道基础知识,但我只是喜欢编程 :D 我已经为以下问题绞尽脑汁 2 天了,不知道你们是否可以帮助我?

我正在为我的比特币/山寨币矿工(特别是 NVIDIA 矿工的 ccminer)编写 GUI 监视器,如果我为其设置了配置,我希望矿工能够跳上最赚钱的代币。 获得这些数字的最简单方法是通过众多 Web API,例如 thisthis。所以你可以看到,有很多 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.Parse 它也不起作用。我是不是在某个地方犯了一个严重的错误?

非常感谢!

【问题讨论】:

    标签: c# json api json.net


    【解决方案1】:

    试试

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

    【讨论】:

    • 不,那没用;)但是 L.B. 的回答为我解决了!感谢您的尝试:D
    【解决方案2】:

    您是否正在尝试做这样的事情?

    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; }
    }
    

    【讨论】:

    • 是的!这就是我最初真正想要的。但是当我尝试设置类时,我不知道字典在这里的用法,所以我最终为每个不同的硬币设置了一个类^^'但这不是动态的,所以我尝试了。这实际上帮助了我很多!谢谢:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    相关资源
    最近更新 更多