CSharp · Console
using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class Program
{
private static string Json = @"
{
'result': {
'14400': [
[
1664006400,
19121.92,
19189,
18925.94,
18997.14,
40878.7735,
780289470.6555521
],
[
1664020800,
18996.63,
19002.18,
18988.8,
18997.46,
153.341,
2912907.7335217
]
],
'180': [
[
1664006400,
18998.8,
19000.05,
18990.05,
18997.14,
319.25744,
6064375.6882797
],
[
1664006580,
18996.63,
19002.18,
18988.8,
18997.46,
153.341,
2912907.7335217
]
],
'1800': [
[
1664006400,
19003.15,
19025.07,
18968.11,
18997.14,
4823.42007,
91655099.2502063
],
[
1664008200,
18996.63,
19002.18,
18988.8,
18997.46,
153.341,
2912907.7335217
]
],
'21600': [
[
1664020800,
19136.9,
19164.25,
18925.94,
18997.46,
20740.99979,
394973767.0314926
]
],
'300': [
[
1664006400,
18984.59,
19001.1,
18980.28,
18997.14,
591.96318,
11243452.571249
],
[
1664006700,
18996.63,
19002.18,
18988.8,
18997.46,
153.341,
2912907.7335217
]
],
'3600': [
[
1664006400,
19078.19,
19084.27,
18925.94,
18997.14,
11653.39145,
221429662.3864255
],
[
1664010000,
18996.63,
19002.18,
18988.8,
18997.46,
153.341,
2912907.7335217
]
],
'60': [
[
1664006400,
18995.8,
18999.1,
18993.77,
18997.14,
106.13386,
2016106.7154772
],
[
1664006460,
18996.63,
19002.18,
18988.8,
18997.46,
153.341,
2912907.7335217
]
],
'7200': [
[
1664006400,
19136.9,
19164.25,
18925.94,
18997.14,
20587.65879,
392060859.2979709
],
[
1664013600,
18996.63,
19002.18,
18988.8,
18997.46,
153.341,
2912907.7335217
]
],
'900': [
[
1664006400,
19008.41,
19017.63,
18968.11,
18997.14,
2341.42806,
44481574.1022888
],
[
1664007300,
18996.63,
19002.18,
18988.8,
18997.46,
153.341,
2912907.7335217
]
]
},
'allowance': {
'cost': 0,
'remaining': 10,
'remainingPaid': 9999999999,
'account': '00000'
}
}";
public static void Main()
{
var result = JsonConvert.DeserializeObject<Root>(Json);
result.Dump();
result.Result[result.Result.Keys.First()].Dump();
}
}
class Root
{
public Dictionary<int, InnerMost[]> Result { get; set; }
public Allowance Allowance { get; set; }
}
class Allowance
{
public int Cost { get; set; }
public int Remaining { get; set; }
public long RemainingPaid { get; set; }
public string Account { get; set; }
}
[JsonConverter(typeof(ArrayToObjectConverter<InnerMost>))]
class InnerMost
{
[JsonArrayIndex(0)]
public long CloseTime { get; set; }
[JsonArrayIndex(1)]
public decimal Open { get; set; }
[JsonArrayIndex(2)]
public decimal High { get; set; }
[JsonArrayIndex(3)]
public decimal Low { get; set; }
[JsonArrayIndex(4)]
public decimal Close { get; set; }
[JsonArrayIndex(5)]
public decimal Volume { get; set; }
[JsonArrayIndex(6)]
public decimal QuoteVolume { get; set; }
}
class JsonArrayIndexAttribute : Attribute
{
public int Index { get; private set; }
public JsonArrayIndexAttribute(int index)
{
Index = index;
}
}
class ArrayToObjectConverter<T> : JsonConverter where T : class, new()
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(T);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JArray array = JArray.Load(reader);
var propsByIndex = typeof(T).GetProperties()
.Where(p => p.CanRead && p.CanWrite && p.GetCustomAttribute<JsonArrayIndexAttribute>() != null)
.ToDictionary(p => p.GetCustomAttribute<JsonArrayIndexAttribute>().Index);
JObject obj = new JObject(array
.Select((jt, i) =>
{
PropertyInfo prop;
return propsByIndex.TryGetValue(i, out prop) ? new JProperty(prop.Name, jt) : null;
})
.Where(jp => jp != null)
);
T target = new T();
serializer.Populate(obj.CreateReader(), target);
return target;
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}