Может ли кто-нибудь сказать мне, почему я получаю ошибку, десериализуя этот ответ JSON?C# .Net 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));
}
}
}
Значение строки возвращается есть: { "1CR": "доступен": "0,00000000", "onOrders": "0,00000000", "btcValue": "0.00000000"}}
I имеют интерфейс для баланса:
public interface IBalance
{
double QuoteAvailable { get; }
double QuoteOnOrders { get; }
double BitcoinValue { get; }
}
и баланс модели:
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; }
}
Хотя это не deserial izing JSON для объекта Balance. Я получаю эту ошибку:
(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.)
Эта ошибка отмечена в коде, где она встречается.
Любые советы?
Я нахожусь гу essing вы вызываете 'DeserializeObject', но он должен быть 'DeserializeObject '. И почему вы не используете 'JsonConvert'? –
Привет, Максим, вы можете показать, как JsonConvert можно использовать в этом контексте? – Nickmccomb