this question is almost identical to the one here
I have tried translating the answer into C#, but I am not an expert in JSON and am a little lost.
I am attempting to deserialize this JSON response from the Kraken OHLC endpoint. Example response, click here
The request pulls back this JSON:
{
"error":[],
"result":
{
"XXBTZEUR":
[
[1679269500,"26401.1","26401.1","26211.6","26243.9","26307.1","8.92959425",311],
[1679270400,"26253.1","26324.7","26060.2","26242.9","26212.0","33.15872129",520],
[1679271300,"26250.1","26276.8","26216.8","26260.4","26259.0","3.63710383",183]
],
"last":1679915700
}}
It’s valid JSON, but I cannot figure out how to handle the "last" field, which seems to throw everything out.
I thought I was close with these Class structures, but no matter what, my inner array returns as null:
public class OHLCResponse
{
[JsonProperty(PropertyName = "error")]
public List<string> Error;
[JsonProperty("result")]
public OHLC Result { get; set; }
}
public class OHLC
{
//[JsonProperty("ohlc_pairs")]
public Dictionary<string, List<OHLCPair>> OHLCPairs { get; set; }
[JsonProperty("last")]
public int Last { get; set; }
}
public class OHLCPair
{
[JsonProperty("time")]
public int UnixDateStamp { get; set; }
[JsonProperty("open")]
public string Open { get; set; }
[JsonProperty("high")]
public string High { get; set; }
[JsonProperty("low")]
public string Low { get; set; }
[JsonProperty("close")]
public string Close { get; set; }
[JsonProperty("vwap")] //volume weighted average price
public string VWAP { get; set; }
[JsonProperty("volume")]
public string Volume { get; set; }
[JsonProperty("count")]
public string Count { get; set; }
}
And I’m just calling it with a standard library that is happily handling most of the other objects into type-safe Classes in their API:
OHLCResponse cOHLCResponse = _CKraken.GetPublicWebResponse<OHLCResponse>("OHLC", param);
… which implements:
result = (T)JsonConvert.DeserializeObject<T>(json);
The result, no matter how I try to alter my type Classes is always an empty array, because I think that it just cannot handle the "last" field:
Can anyone point me in the right direction? I was unfortunately not able to translate the custom deserializer in the previous question. Many thanks in advance, Dave.
2
Answers
Wow, quite a challenging question.
I think you can achieve this by implementing the custom JSON converter for the
OHLC
andOHLCPair
classes.JsonConverter
with theJsonConverter
attribute for the respective property.In
OHLCPairConverter
, it aims to convert the key-value pair other than thelast
field toDictionary<string, List<OHLCPair>>
and assign it to theOHLCPairs
property.In
OHLCPairConverter
, it aims to convert the array (JArray
) with multiple values and types to anOHLCPair
instance.Anyway, for the
WriteJson
methods, you can achieve to write JSON (serialize) by deciding the format, the property, and its value to be printed without the need of System.Reflection. My approach seems to over-complicated.you just need a very simple converter
and change JsonProperty attributes of OHLCPair class