将JSON反序列化到类(Newtonsoft,C#)

将JSON反序列化到类(Newtonsoft,C#),c#,json,json.net,json-deserialization,C#,Json,Json.net,Json Deserialization,我试图将JSON数据保存到我的类中。我尝试过不同的方法,但没有成功。 这是我的班级: class YoCurrency { public string Currency { get; set; } public double high { get; set; } public double low { get; set; } public double avg { get; set; } public double vol { get; set; }

我试图将JSON数据保存到我的类中。我尝试过不同的方法,但没有成功。 这是我的班级:

class YoCurrency
{
    public string Currency { get; set; }
    public double high { get; set; }
    public double low { get; set; }
    public double avg { get; set; }
    public double vol { get; set; }
    [JsonProperty("vol_cur")]
    public double vol_cur { get; set; }
    public double last { get; set; }
    public double buy { get; set; }
    public double sell { get; set; }
    public int updated { get; set; }
}
这是
正在尝试使用泛型列表反序列化:

List<YoCurrency> a = JsonConvert.DeserializeObject<List<YoCurrency>>(json);
但它没有产生任何积极的结果。我尝试使用生成c#类,但它为每种货币生成一个类:

public class BtcUsd
{
    public double high { get; set; }
    public double low { get; set; }
    public double avg { get; set; }
    public double vol { get; set; }
    public double vol_cur { get; set; }
    public double last { get; set; }
    public double buy { get; set; }
    public double sell { get; set; }
    public int updated { get; set; }
}

public class EthUsd
{
    ...
}
public class RootObject
{
    public BtcUsd btc_usd { get; set; }
    public EthUsd eth_usd { get; set; }
    ...
}
但我认为,对于50或500对货币来说,这不是一个好方法(或者我应该为每种货币创建一个唯一的类吗?)


所以请帮我反序列化这个JSON,或者给我一些可以帮助我解决这个问题的信息

为什么不上一节课呢

class Program
{
    static void Main(string[] args)
    {
        var array = "[{\"btc_usd\":[{\"high\":8550.0102,\"low\":7800,\"avg\":8175.0051,\"vol\":1615543.57397705,\"vol_cur\":197.54076079,\"last\":7850,\"buy\":7850.00000000,\"sell\":7879.00000000,\"updated\":1521383863}],\"eth_usd\":[{\"high\":622.93708559,\"low\":482,\"avg\":552.46854279,\"vol\":346598.40520112,\"vol_cur\":630.37075493,\"last\":488.27857735,\"buy\":489.77564548,\"sell\":492.11726255,\"updated\":1521383876}]}]";
        List<RootObject> a = JsonConvert.DeserializeObject<List<RootObject>>(array);
    }
}
public class RootCurrency
{
    public double high { get; set; }
    public int low { get; set; }
    public double avg { get; set; }
    public double vol { get; set; }
    public double vol_cur { get; set; }
    public double last { get; set; }
    public double buy { get; set; }
    public double sell { get; set; }
    public int updated { get; set; }
}

public class RootObject
{
    public List<RootCurrency> btc_usd { get; set; }
    public List<RootCurrency> eth_usd { get; set; }
}
类程序
{
静态void Main(字符串[]参数)
{
目前,780,”avg\:7800,,“avg\”:8175.0051,,“vol\”:8175.0051,,“vol\”,,“vol\”:161555543.5739775,,,,“btc-U美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元兑美元:197.5407.5407607607607607.76076079,最后一个“最后一”最后一点:7850,最后一个“最后一个”最后一点:7850,买买买买价:7850.亿亿亿亿亿万万万万万万万万万万万,卖卖卖卖:7879.79.亿亿亿亿万万万万万万万万万万万万万,卖卖卖卖卖卖卖卖卖卖:7879.7879.7879.79.亿亿亿亿亿20112,“vol\u cur\”:630.37075493,“last”\“:488.27857735,“买入”:489.77564548,“卖出”:492.11726255,“更新”:1521383876}]”;
列表a=JsonConvert.DeserializeObject(数组);
}
}
公共类根货币
{
公共双高{get;set;}
公共整数低{get;set;}
公共双平均值{get;set;}
公共双卷{get;set;}
公共双卷{get;set;}
公共双最后{get;set;}
公共双买{get;set;}
公共双重出售{get;set;}
公共int更新{get;set;}
}
公共类根对象
{
公共列表btc_usd{get;set;}
公共列表eth_usd{get;set;}
}

看起来您的类结构不正确,建议您在Visual Studio的帮助下生成类,如下所示,并使用生成的类尝试您的代码

基于json结构,您需要这样的类设计

public class Rootobject
{
   // do List<CurrencyDetail> if you are expecting multiple element
    public CurrencyDetail btc_usd { get; set; }
    public CurrencyDetail eth_usd { get; set; }
    public CurrencyDetail doge_usd { get; set; }
    public CurrencyDetail pac_usd { get; set; }
    public CurrencyDetail rdd_usd { get; set; }
}

public class CurrencyDetail
{
    public float high { get; set; }
    public int low { get; set; }
    public float avg { get; set; }
    public float vol { get; set; }
    public float vol_cur { get; set; }
    public float last { get; set; }
    public float buy { get; set; }
    public float sell { get; set; }
    public int updated { get; set; }
}
公共类根对象
{
//如果需要多个元素,请列出
公共货币详细信息btc_usd{get;set;}
公共货币详细信息eth_usd{get;set;}
公共货币详细信息doge_usd{get;set;}
公共货币详细信息PACU usd{get;set;}
公共货币详细信息rdd_usd{get;set;}
}
公共类CurrencyDetail
{
公共浮点高{get;set;}
公共整数低{get;set;}
公共浮点平均值{get;set;}
公共浮动卷{get;set;}
公共浮点volu cur{get;set;}
公共浮点最后{get;set;}
公共浮动买入{get;set;}
公开浮动卖出{get;set;}
公共int更新{get;set;}
}
我的建议是,如果可能的话,修改json,它将包含比您的类结构更详细的货币类型

public class Rootobject
{
    public List<CurrencyDetail> currencies { get; set; }
}

public class CurrencyDetail
{
    //btc_usd  or  eth_usd or doge_usd  or pac_usd 
    public string type { get; set; }
    public float high { get; set; }
    public int low { get; set; }
    public float avg { get; set; }
    public float vol { get; set; }
    public float vol_cur { get; set; }
    public float last { get; set; }
    public float buy { get; set; }
    public float sell { get; set; }
    public int updated { get; set; }
}
公共类根对象
{
公共列表货币{get;set;}
}
公共类CurrencyDetail
{
//btc_美元或eth_美元或doge_美元或pac_美元
公共字符串类型{get;set;}
公共浮点高{get;set;}
公共整数低{get;set;}
公共浮点平均值{get;set;}
公共浮动卷{get;set;}
公共浮点volu cur{get;set;}
公共浮点最后{get;set;}
公共浮动买入{get;set;}
公开浮动卖出{get;set;}
公共int更新{get;set;}
}

例如,将JSON粘贴到中,查看得到的对象。然后在尝试时反序列化它
public class Rootobject
{
   // do List<CurrencyDetail> if you are expecting multiple element
    public CurrencyDetail btc_usd { get; set; }
    public CurrencyDetail eth_usd { get; set; }
    public CurrencyDetail doge_usd { get; set; }
    public CurrencyDetail pac_usd { get; set; }
    public CurrencyDetail rdd_usd { get; set; }
}

public class CurrencyDetail
{
    public float high { get; set; }
    public int low { get; set; }
    public float avg { get; set; }
    public float vol { get; set; }
    public float vol_cur { get; set; }
    public float last { get; set; }
    public float buy { get; set; }
    public float sell { get; set; }
    public int updated { get; set; }
}
public class Rootobject
{
    public List<CurrencyDetail> currencies { get; set; }
}

public class CurrencyDetail
{
    //btc_usd  or  eth_usd or doge_usd  or pac_usd 
    public string type { get; set; }
    public float high { get; set; }
    public int low { get; set; }
    public float avg { get; set; }
    public float vol { get; set; }
    public float vol_cur { get; set; }
    public float last { get; set; }
    public float buy { get; set; }
    public float sell { get; set; }
    public int updated { get; set; }
}