正在反序列化JSON中的混合类型、无名称数组



我正在尝试为这个WEB-API方法编写C#类型和反序列化代码,使用C#的Newtonsoft JSON库:https://docs.kraken.com/rest/#operation/getTickerInformation

示例JSON如下所示:

{
"error": [],
"result": {
"XXBTZUSD": {
"a": [
"52609.60000",
"1",
"1.000"
],
"b": [
"52609.50000",
"1",
"1.000"
],
"c": [
"52641.10000",
"0.00080000"
],
"v": [
"1920.83610601",
"7954.00219674"
],
"p": [
"52389.94668",
"54022.90683"
],
"t": [
23329,
80463
],
"l": [
"51513.90000",
"51513.90000"
],
"h": [
"53219.90000",
"57200.00000"
],
"o": "52280.40000"
}
}
}

对于其他API方法,我使用如下代码直接映射到我的类型:

public static Ticker GetTicker(string pair)
{
try
{
return Deserialize<Ticker>GetJObject($"https://api.kraken.com/0/public/Ticker?pair={pair}");

}
catch
{
throw;
}
}
private static T Deserialize<T>(JObject jObject)
{
try
{
var err = jObject["error"];
if (err.HasValues)
{
throw new Exception("Kraken Error" + String.Join(',',err.ToObject<string[]>()));
}
return jObject["result"].ToObject<T>();
}
catch
{
throw;
}
}

但在这种情况下,JSON在任意数组中返回无名称值,而且Ticker嵌套在由pair命名的另一个元素下。更糟糕的是,数组使用混合类型。所以我(到目前为止(求助于这样的东西:

public class Ticker
{
[JsonProperty("a")]
public float[] ask { get; set; }
[JsonProperty("b")]
public float[] bid { get; set; }
[JsonProperty("c")]
public float[] close { get; set; }
[JsonProperty("v")]
public float[] volume { get; set; }
[JsonProperty("p")]
public float[] volumeWeightedPrice { get; set; }
[JsonProperty("t")]
public int[] trades { get; set; }
[JsonProperty("l")]
public float[] low { get; set; }
[JsonProperty("h")]
public float[] high { get; set; }
[JsonProperty("o")]
public float opening { get; set; }
}
}

我是NewtonSoft的JSON库的新手,想知道是否可以让它纯粹用于Ticker类声明,大概是使用注释?或者,如果我必须深入研究自定义反序列化代码。。。那会是什么样子?

型号:

public class Ticker
{
[JsonProperty("a")]
public float[] ask { get; set; }
[JsonProperty("b")]
public float[] bid { get; set; }
[JsonProperty("c")]
public float[] close { get; set; }
[JsonProperty("v")]
public float[] volume { get; set; }
[JsonProperty("p")]
public float[] volumeWeightedPrice { get; set; }
[JsonProperty("t")]
public int[] trades { get; set; }
[JsonProperty("l")]
public float[] low { get; set; }
[JsonProperty("h")]
public float[] high { get; set; }
[JsonProperty("o")]
public float opening { get; set; }
}
public class Result
{
[JsonProperty("XXBTZUSD")]
public Ticker Ticker { get; set; }
}
public class Root
{
public List<object> error { get; set; }
public Result result { get; set; }
}

解密:

Root r = JsonConvert.DeserializeObject<Root>(json_text);
Ticker ticker = r.result.Ticker;

访问:
ticker.ask[0] //52609,6

相关内容

  • 没有找到相关文章

最新更新