Bombadil
Bombadil

Reputation: 15

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!

Upvotes: 0

Views: 325

Answers (2)

L.B
L.B

Reputation: 116178

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

Upvotes: 0

user1108948
user1108948

Reputation:

try

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

Upvotes: 1

Related Questions