有人可以告诉我为什么我收到反序列化此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));
}
}
}
返回的字符串值是: {&#34; 1CR&#34;:&#34;购&#34;:&#34; 0.00000000&#34;&#34; onOrders&#34;:&#34; 0.00000000&#34;&# 34; btcValue&#34;:&#34; 0.00000000&#34;}}
我有一个平衡界面:
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; }
}
虽然它没有将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.)
此错误会在出现的代码中注明 有什么提示吗?
答案 0 :(得分:1)
我可以在你的JSON字符串中看到两个紧密的大括号:
{"1CR":"available":"0.00000000","onOrders":"0.00000000","btcValue":"0.00000000"}}
使用正确的JSON格式字符串来源JSON.DeserializeObject<T>
,我认为它应该有效。
答案 1 :(得分:0)
Maxim Kosov的评论和Kunal Chitkara的回答似乎都是正确的,我会在字符串中添加第一个标记
"1CR":"available":"0.00000000"
似乎也不正确,也许它应该只是
"available":"0.00000000"
在原始代码中,评论提到这是错误的可能原因:
路径'1CR.available',第1行,第20位。