为什么我的反序列化给了我一个错误Newtonsoft.Json.JsonConvert?

时间:2017-11-28 21:16:25

标签: c# arrays json

public List<CoinMarket> GetCoinMarket()
{
    List<CoinMarket> coinMarket = new List<CoinMarket>();
    var URLWebAPI = "http://190.202.54.19/wsZeus/api/Account/Markets/Get";
    try
    {
        using (var Client = new System.Net.Http.HttpClient())
        {
            var JSON =  Client.GetStringAsync(URLWebAPI);
            coinMarket = (List<CoinMarket>)Newtonsoft.Json.JsonConvert.DeserializeObject(JSON.Result);
        }
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(@"    ERROR {0}", ex.Message);
    }
    return coinMarket;
}

投掷,我不知道为什么。看起来序列化部分有问题。但我验证了它。

3 个答案:

答案 0 :(得分:1)

您正在错误地使用json反序列化程序。 DeserializeObject会返回不能投放到List<T>的自定义对象。这个输出:

Newtonsoft.Json.Linq.JArray
SO20171129.CoinData[]
System.Collections.Generic.List`1[SO20171129.CoinData]

是此代码的结果。

class Program
{
    static void Main(string[] args)
    {
        // returns Newtonsoft.Json.Linq.JArray
        var coinMarket = Newtonsoft.Json.JsonConvert.DeserializeObject(File.ReadAllText("get.json"));
        Console.WriteLine(coinMarket.GetType());
        // returns array of CoinData
        var coinMarketTyped = Newtonsoft.Json.JsonConvert.DeserializeObject<CoinData[]>(File.ReadAllText("get.json"));
        Console.WriteLine(coinMarketTyped.GetType());
        // returns List of CoinData
        var coinMarketTyped2 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<CoinData>>(File.ReadAllText("get.json"));
        Console.WriteLine(coinMarketTyped2.GetType());
    }
}

public class CoinData
{
    public string id { get; set; }
    public string name { get; set; }
    public string symbol { get; set; }
    public string rank { get; set; }
    public string price_usd { get; set; }
    public string price_btc { get; set; }
    public string __invalid_name__24h_volume_usd { get; set; }
    public string market_cap_usd { get; set; }
    public string available_supply { get; set; }
    public string total_supply { get; set; }
    public string percent_change_1h { get; set; }
    public string percent_change_24h { get; set; }
    public string percent_change_7d { get; set; }
    public string last_updated { get; set; }
}

答案 1 :(得分:0)

我的猜测是你的json结构成员与CoinMarket类成员不匹配。

我要做的是复制json结果并在以下网站上生成相应的CoinMarket类:http://json2csharp.com/

它将为您输出正确的CoinMarket课程。

答案 2 :(得分:0)

 public class CoinMarket
{
    public string id { get; set; }
    public string name { get; set; }
    public string symbol { get; set; }
    public string rank { get; set; }
    public string price_usd { get; set; }
    public string price_btc { get; set; }
    [JsonProperty("24h_volume_usd")]
    public string h_volume_usd { get; set; }
    public string market_cap_usd { get; set; }
    public string available_supply { get; set; }
    public string total_supply { get; set; }
    public string percent_change_1h { get; set; }
    public string percent_change_24h { get; set; }
    public string percent_change_7d { get; set; }
    public string last_updated { get; set; }

}

}

这是我的CoinMarket Class