简体   繁体   中英

C# can't read and interpret web API/JSON

My first question here, probably because I found loads of previous questions & answers here :) I'm just a hobby programmer, I know only the basics, but I just love programming :D I've been cracking my head over the following problem for 2 days now, and I wonder if you guys could help me?

I'm programming a GUI monitor for my bitcoin/altcoin miners (specifically, ccminer for NVIDIA miners), and I want a miner to be able to jump on the most profitable coin if I have set a config for it. The easiest way to get these numbers would be trough the numerous web API's, like this and this . So you can see, there are numerous API's (would've linked more, but not allowed yet), but none of them seems to work.

This is the code I have so far:

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, I'm using the debug mode just to look at what's inside the objects but when I try to use my method at this api (or any other with corresponding elements) but at

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

It errors out. I don't know where to find the exact error too, but even when I'm trying JArray.Parse it just doesn't work. Am I making a crucial mistake somewhere?

Many thanks in advance!

尝试

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

Are you trying to do something like this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM