C#JSON反序列化:类型是接口或抽象类,无法实例化 [英] C# JSON Deserialization : Type is an interface or abstract class and cannot be instantiated

查看:148
本文介绍了C#JSON反序列化:类型是接口或抽象类,无法实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用这个C#项目,该项目使用 API与在线交易平台Poloniex通信.

I'm using this C# project which uses APIs to communicate with the online trading platform Poloniex.

该代码应该可以在钱包中余额:

This code is supposed to get balances in a wallet:

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

尽管此代码给出此错误:

Although this code gives this error:

获取钱包时出错:无法创建类型为Jojatekok.PoloniexAPI.WalletTools.IBalance的实例.
类型是接口或抽象类,无法实例化.
路径"1CR.available",第1行,位置20.

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

in Helper.cs here:

[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响应时遇到错误吗?

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

响应是JSON,下面是示例:

The response is JSON, here is a sample of it:

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

推荐答案

这是我为使其正常运行所做的工作.感谢Guru.com的Chris使此工作正常进行.很棒的人,而且价格也很合理.您可以在这里找到他: http://www.guru.com/freelancers/deatc

This is what i have done to get it working. Thanks to Chris from Guru.com for getting this working. Great guy and very well priced. You can find him here: 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;
}

这篇关于C#JSON反序列化:类型是接口或抽象类,无法实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆