C# JSON 反序列化:类型是接口或抽象 class,无法实例化

C# JSON Deserialization : Type is an interface or abstract class and cannot be instantiated

我正在使用这个使用 APIs to communicate with the online trading platform Poloniex 的 C# 项目。

此代码应该获取钱包中的余额:

var x = await polo_client.Wallet.GetBalancesAsync();

虽然这段代码给出了这个错误:

Error getting wallet: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.

在 Helper.cs 此处:

[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))
        {
            return (T)serializer.Deserialize(jsonTextReader, typeof(T));
        }
    } 
}

调用它的代码是:

public T GetData<T>(string command, params object[] parameters)
{
    var relativeUrl = CreateRelativeUrl(command, parameters);
    var jsonString = QueryString(relativeUrl);
    var output = JsonSerializer.DeserializeObject<T>(jsonString);
    return output;
}

谁能告诉我为什么反序列化此 JSON 响应时出错?

响应是 JSON,这是它的一个示例:

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

JSON与否,错误自述。您不能实例化接口或抽象 class。它们只是它们所代表的功能或对象结构的蓝图。

例如,您不能这样做:

var something = new ISomething();

但是,你可以这样做:

ISomething something = new Something();

您所需要的只是该接口的一些具体实现。类似于:

public JsonReceived : IBalance
{
// rest of the implementation
}

或者可能已经由第三方提供,请查看他们的 SDK 文档。

由于对象的反序列化导致的错误,它在内部创建了一个 Jobject 并将其转换为输入通用,在上述情况下,由于我们无法创建 Interface 的对象,因此它会抛出错误, 我们可以根据您在GitHub

中发布的解决方案通过以下方式实现

  • 添加哈希表作为 PostData 方法的通用输入
  • 将您的 wallet.cs 更改为 例如:

    IBalance CreateFactory(string typeOfBalance, string jsonString)
            {
                IBalance balance = null;
                switch (typeOfBalance)
                {
                    case "balanceClassIdentification":
                        balance = new Balance();
                        balance = Newtonsoft.Json.JsonConvert.DeserializeObject<Balance>(jsonString);
                        break;
                    case "":
                    // write logic if Ibalance interface implemented in any childClass ex: class stockBalance: IBalance , then balance = new StockBalance();balance = Newtonsoft.Json.JsonConvert.DeserializeObject<stockBalance>(jsonString);
                    default:
                        balance = new Balance();
                        balance = Newtonsoft.Json.JsonConvert.DeserializeObject<Balance>(jsonString);
                        break;
                }
                return balance;
            }
            private IDictionary<string, IBalance> GetBalances()
            {
                IDictionary<string, IBalance> data = null;
                var postData = new Dictionary<string, object>();
                //var data = PostData<IDictionary<string, object>>("returnCompleteBalances", postData);
    
                Hashtable output = PostData<Hashtable>("returnCompleteBalances", postData);
                if (output.Count > 0)
                {
    
                    data = new Dictionary<string, IBalance>();
                }
                foreach (string jsonObjKey in output.Keys)
                {
                    IBalance balace = CreateFactoty("balanceClassIdentification", output[jsonObjKey].ToString()); // this "balanceClassIdentification" just used as a string but we can add the condition in createFactory according to the type of object that wallet
    
                    data.Add(jsonObjKey, balace);
                }
                return data;
            }
    

  • 创建另一个实现 Ibalance
  • 的中间体 class 喜欢

    public class BalanceBase: IBalace
     {
     } 
    

    需要更改余额class

     public Balance : BalanceBase
        {
        }
    

  • if 除 balance
  • 外,没有为 IBalance 实现其他 classes 您可以直接使用 return 类型的 GetBalance() 方法 作为词典 并更改实现

     private IDictionary<string, Balance> GetBalances()
            {
    
                var postData = new Dictionary<string, object>();
                var data = PostData<IDictionary<string, Balance>>("returnCompleteBalances", postData);
    
                return data;
            }
    

    这就是我为让它工作所做的工作。感谢来自 Guru.com 的 Chris 让这项工作顺利进行。很棒的人,价格也很合理。你可以在这里找到他:http://www.guru.com/freelancers/deatc

    private IDictionary<string, IBalance> GetBalances()
    {
        var postData = new Dictionary<string, object>();
        var data = PostData<IDictionary<string, Balance>>("returnCompleteBalances", postData);
        var returnData = new Dictionary<string, IBalance>();
    
        foreach (string key in data.Keys)
        {
            returnData.Add(key, data[key]);
        }
    
        return returnData;
    }