C# 无法读取和解释 Web API/JSON [英] C# can't read and interpret web API/JSON

查看:39
本文介绍了C# 无法读取和解释 Web API/JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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.

这是我目前的代码:

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(或任何其他具有相应元素的)上使用我的方法时,但在

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"];

它出错了.我也不知道在哪里可以找到确切的错误,但即使我在尝试 JArray.Parse 它也不起作用.我是不是在某个地方犯了一个严重的错误?

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?

非常感谢!

推荐答案

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

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

这篇关于C# 无法读取和解释 Web API/JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆