I have a response from some Rest API that just returns some array of arrays like the following:
[
[123,"0.01","0.02","0.03","0.04","12345.00000",123456789,"300.000",4000,"123.000","456.000","0"],
[456,"0.04","0.03","0.02","0.01","54321.00000",987654321,"500.000",4000,"123.000","456.000","1"],
[789,"0.05","0.06","0.07","0.08","12345.00000",123456789,"700.000",8000,"456.000","123.000","0"]
]
In this example, the amount of datasets is 3, but the amount is always different and could be 100+ also.
I want to have this read out into a class object, which has 12 arrays according to each type of value shown in the response:
public class foo
{
...
public int[] firstParam;
public string[] secondParam;
public string[] thirdParam;
...
}
For example, firstParam
should contain then {123,456,789}
; secondParam
should contain {"0.01","0.04","0.05"}
and so on.
The schema for the columns is known and documented in Public Rest API for Binance: Kline/Candlestick data.. An example would be some query like https://api.binance.com/api/v1/klines?symbol=XVGBTC&interval=1h
The API response is perfectly valid JSON; it is a jagged 2d array of primitive values where the columns have specific meanings as defined in the Public Rest API for Binance: Kline/Candlestick data. As such it can be parsed and deserialized using json.net, e.g. as an
object [][]
:(Sample working .Net fiddle #1.)
However, rather than deserializing the JSON into a jagged 2d object array or (as you suggest in your question) a single root object with array properties corresponding to column values, I would recommend that you design a class
BinanceKlineData
that represents a single row of values for those specific columns, then deserialize into aList<BinanceKlineData>
using the customJsonConverter
ObjectToArrayConverter<BinanceKlineData>
from C# JSON.NET - Deserialize response that uses an unusual data structure.Firstly, using the documented meanings of the columns, you can define your type
BinanceKlineData
as follows:Notice that I have annotated the properties with
[JsonProperty(Order = N)]
. This order corresponds to the order of the columns in the Rest API, and will be used to inform Json.NET how to map columns to properties by column index. Notice also that I modeled the numeric columns asdecimal
despite the fact that they appear as strings in the JSON. You could usestring
if you prefer a more literal deserialization.Next, grab the generic
ObjectToArrayConverter<T>
from this answer. It has logic to make use of theOrder = N
metadata to map row values to member values in the generic typeT
by column index.Finally, you will be able to deserialize the JSON as a
List<BinanceKlineData>
as follows:Sample working .Net fiddle #2.
Or if you prefer you could apply the converter directly to
BinanceKlineData
as follows:When the converter is applied directly to the type it is no longer necessary to pass it in via
JsonSerializerSettings.Converters
.Sample fiddle #3.
Depending on exactly how the response is formatted you might be able to do something like this: