简体   繁体   中英

C#.Net JSON Serialization not working

Can someone tell me why i'm getting an error deserializing this JSON response?

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

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. 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:

{"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.

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

"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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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