Yobit c#,jsonconvert.deserialize

时间:2017-11-14 15:31:35

标签: c# json.net json-deserialization

来自https://yobit.net/api/3/depth/btc_usd?limit=5我有

    string response = { "btc_usd":{ "asks":[[6657,0.06689384],[6680.47685546,0.001],[6680.47690546,0.00526334],[6697,0.05],[6698,0.05]],"bids":[[6656,0.02371773],[6640.00000001,0.0297727],[6640,0.34752146],[6637.46352566,0.0011574],[6634,0.00424518]]}}

我想反序列化这个。 我创建了一些类

public class Orderbook
{
    [JsonProperty("asks")]
    public List<BidAsk> asks { get; set; }

    [JsonProperty("bids")]
    public List<BidAsk> bids { get; set; }
}   


public class BidAsk
{
    public decimal price { get; set; }
    public decimal volume { get; set; }
}

var jsonResponse = JsonConvert.DeserializeObject<Orderbook>(response);

完成此操作后,我得到: jsonResponse.asks = null jsonResponse.bids = null

我在反序列化方面做错了什么?

2 个答案:

答案 0 :(得分:2)

货币有额外的水平。尝试类似:

public class Orderbook
{
    [JsonProperty("btc_usd")]
    public Currency BTCToUSD{ get; set; }
}  

public class Currency
{
    [JsonProperty("asks")]
    public double[][] asks { get; set; }

    [JsonProperty("bids")]
    public double[][] bids { get; set; }
}

var jsonResponse = JsonConvert.DeserializeObject<Orderbook>(response);

注意我已经将出价/要求更改为双打数组的数组,就像它们在你的例子中一样。

您可能也想查看decimal,不确定Json.NET如何处理它,但在处理财务时,您可能希望对浮动精度保持谨慎。

答案 1 :(得分:0)

如果我不知道这些答案如何反序列化,我得到了哪一对? 例如,两个不同的请求:

1)https://yobit.net/api/3/depth/btc_usd?limit=5

2)https://yobit.net/api/3/depth/eth_usd?limit=5

我应该为另一对“eth_usd”创建一个新的JsonProperty吗?

.as-console-wrapper { max-height: 100% !important; top: 0; }

在这种情况下,在每个请求中,类的所有其他属性都将为null。 我想得到这样的东西:

[JsonProperty("eth_usd")]
public Currency ETHToUSD{ get; set; }
相关问题