简体   繁体   English

C#.Net JSON序列化不起作用

[英]C#.Net JSON Serialization not working

Can someone tell me why i'm getting an error deserializing this JSON response? 有人可以告诉我为什么我在反序列化此JSON响应时遇到错误吗?

    public T PostData<T>(string command, Dictionary<string, object> postData)
    {
        postData.Add("command", command);
        postData.Add("nonce", Helper.GetCurrentHttpPostNonce());

        var jsonString = PostString(Helper.ApiUrlHttpsRelativeTrading, postData.ToHttpPostString());
        var output = JsonSerializer.DeserializeObject<T>(jsonString);

        return output;
    }

    [SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
    internal static T DeserializeObject<T>(this JsonSerializer serializer, string value)
    { 
        using (var stringReader = new StringReader(value)) {
            using (var jsonTextReader = new JsonTextReader(stringReader)) {
                /*   
                    An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
                    Additional information: Could not create an instance of type Jojatekok.PoloniexAPI.WalletTools.IBalance. Type is an interface or abstract class and cannot be instantiated. Path '1CR.available', line 1, position 20. 

                    ERROR's ON NEXT LINE :
                */
                return (T)serializer.Deserialize(jsonTextReader, typeof(T));
            }
        }
    }

The string value coming back is: {"1CR":"available":"0.00000000","onOrders":"0.00000000","btcValue":"0.00000000"}} 返回的字符串值是:{“ 1CR”:“ available”:“ 0.00000000”,“ onOrders”:“ 0.00000000”,“ btcValue”:“ 0.00000000”}}

I have an interface for the balance: 我有一个用于余额的接口:

public interface IBalance
{
    double QuoteAvailable { get; }
    double QuoteOnOrders { get; }
    double BitcoinValue { get; }
}

And a balance model: 以及一个余额模型:

public class Balance : IBalance
{
    [JsonProperty("available")]
    public double QuoteAvailable { get; private set; }
    [JsonProperty("onOrders")]
    public double QuoteOnOrders { get; private set; }
    [JsonProperty("btcValue")]
    public double BitcoinValue { get; private set; }
}

Although it's not deserializing the JSON to a Balance object. 尽管它没有将JSON反序列化为Balance对象。 i'm getting this error: 我收到此错误:

(An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code
Additional information: Could not create an instance of type Jojatekok.PoloniexAPI.WalletTools.IBalance. Type is an interface or abstract class and cannot be instantiated. Path '1CR.available', line 1, position 20.)

This error is noted in the code where it occurs. 该错误在发生的代码中注明。
Any tips? 有小费吗?

I can see two close braces in your JSON string coming back: 我可以看到您的JSON字符串中出现了两个大括号:

{"1CR":"available":"0.00000000","onOrders":"0.00000000","btcValue":"0.00000000"}}

Source the JSON.DeserializeObject<T> it with a correct JSON Format string and I think it should work. 使用正确的JSON格式字符串来源JSON.DeserializeObject<T> ,我认为它应该可以工作。

Both the comment by Maxim Kosov and the answer by Kunal Chitkara seem correct, and I would add that the first token in the string Maxim Kosov的评论和Kunal Chitkara的回答似乎都是正确的,我要补充一下字符串中的第一个标记

"1CR":"available":"0.00000000"

seems also incorrect, maybe it should be only 似乎也不正确,也许应该只是

"available":"0.00000000"

In your original code, the comment mentions this as the probable cause of the error: 在您的原始代码中,注释将其提及为错误的可能原因:

Path '1CR.available', line 1, position 20. 路径“ 1CR.available”,第1行,位置20。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM