简体   繁体   English

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

[英]Why my deserialization is giving me an error Newtonsoft.Json.JsonConvert?

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

It is throwing and i do not know why. 它在扔,我不知道为什么。 It looks like there is something wrong with the serialization part. 序列化部分似乎出了点问题。 But i verified it. 但是我证实了。

You are using json deserializer incorrectly. 您使用的json解串器不正确。 DeserializeObject returns custom object which is not castable to your List<T> . DeserializeObject返回无法投射到List<T>自定义对象。 This output: 此输出:

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

is the result of this code. 是此代码的结果。

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

My guess is that your json structure members do not match the CoinMarket class members. 我的猜测是您的json结构成员与CoinMarket类成员不匹配。

What I would do is to copy the json result and generate the corresponding CoinMarket class on the following website: http://json2csharp.com/ 我要做的是在以下网站上复制json结果并生成相应的CoinMarket类: http ://json2csharp.com/

It will output the right CoinMarket class for you. 它将为您输出正确的CoinMarket类。

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

}

} }

This is my CoinMarket Class 这是我的CoinMarket课程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的项目中出现 Newtonsoft.Json.JsonConvert 的 CS0433 错误? - Why is there a CS0433 error for Newtonsoft.Json.JsonConvert in my project? 尝试扩展Newtonsoft.Json.JsonConvert的SerializeObject方法 - Trying to extend SerializeObject method of Newtonsoft.Json.JsonConvert Newtonsoft.Json.JsonConvert(object) 返回 null object - Newtonsoft.Json.JsonConvert(object) returns null object 由 Newtonsoft.Json.JsonConvert 从同一个类序列化 nad 反序列化 - Serialize nad Deserialize from this same Class by Newtonsoft.Json.JsonConvert 无法从程序集 Newtonsoft.Json 加载类型“Newtonsoft.Json.JsonConvert” - Could not load type 'Newtonsoft.Json.JsonConvert' from assembly Newtonsoft.Json &#39;Newtonsoft.Json.dll&#39;和&#39;NuGetApi2.dll&#39;中都存在&#39;Newtonsoft.Json.JsonConvert&#39;类型 - The type 'Newtonsoft.Json.JsonConvert' exists in both 'Newtonsoft.Json.dll' and 'NuGetApi2.dll' Can I covert python class to dictionary or json like Newtonsoft.Json.JsonConvert() in C# - Can I covert python class to dictionary or json like Newtonsoft.Json.JsonConvert() in C# 即使使用Newtonsoft.Json.JsonConvert进行序列化,REST服务也会收到(400)错误的特殊字符请求 - Rest service getting (400) Bad Request for special characters even after serializing with Newtonsoft.Json.JsonConvert newtonsoft json反序列化错误处理:部分反序列化 - newtonsoft json deserialization error handling: partial deserialization NewtonSoft.Json JsonConvert反序列化错误 - NewtonSoft.Json JsonConvert Deserialize error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM