I'm unable to parse the JSON file below to the corresponding class. It was exported by pandas' DateFrame. The reason I can't parse it, is because the format is a bit weird. How can I do that?
private static List<BacktestResult> LoadFromJson(string path)
{
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
if (!File.Exists(filePath))
throw new FileNotFoundException($"The data file '{filePath}' was not found.");
var data = File.ReadAllText(filePath);
var deserializedData = JsonConvert.DeserializeObject<List<BacktestResult>>(data);
return deserializedData;
}
It works but I want the Pair to be parsed without the / and SellReason to be parsed as SellType (the enum). OpenDate and CloseDate cannot be parsed directly to DateTime, because they have to be converted through the following DateTimeOffset.FromUnixTimeMilliseconds(OpenDate).
public class BacktestResult
{
public string Pair { get; set; }
public decimal ProfitPercentage { get; set; }
public decimal ProfitAbs { get; set; }
public decimal OpenRate { get; set; }
public decimal CloseRate { get; set; }
public DateTime OpenDate { get; set; }
public DateTime CloseDate { get; set; }
public decimal OpenFee { get; set; }
public decimal CloseFee { get; set; }
public decimal Amount { get; set; }
public decimal TradeDuration { get; set; }
public bool OpenAtEnd { get; set; }
public SellType SellReason { get; set; }
}
public enum SellType
{
None,
TakeProfit,
StopLoss,
TrailingStopLoss,
SellSignal
}
JSON:
https://pastebin.com/KHH2fgm7 (pastebin because it's exceeds the SO limits)
Edit:
Thanks to @Panagiotis Kanavos, the JSON now looks like: https://pastebin.com/jNdRC23k. I exported it from pandas as following: results.to_json(r'dateFrame_json.json', orient='split').
public class BacktestResult2
{
[JsonProperty("pair")]
public string Pair { get; set; }
[JsonProperty("profit_percent")]
public decimal ProfitPercentage { get; set; }
[JsonProperty("profit_abs")]
public decimal ProfitAbs { get; set; }
[JsonProperty("open_rate")]
public decimal OpenRate { get; set; }
[JsonProperty("close_rate")]
public decimal CloseRate { get; set; }
[JsonProperty("open_date")]
public long OpenDate { get; set; }
[JsonProperty("close_date")]
public long CloseDate { get; set; }
[JsonProperty("open_fee")]
public decimal OpenFee { get; set; }
[JsonProperty("close_fee")]
public decimal CloseFee { get; set; }
[JsonProperty("amount")]
public decimal Amount { get; set; }
[JsonProperty("trade_duration")]
public decimal TradeDuration { get; set; }
[JsonProperty("sell_reason")]
public SellReason SellReason { get; set; }
}
public partial class SellReason
{
[JsonProperty("name")]
public string Name { get; set; }
}
private static List<BacktestResult> LoadFromJson(string path)
{
var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
if (!File.Exists(filePath))
throw new FileNotFoundException($"The JSON file '{filePath}' was not found.");
var data = File.ReadAllText(filePath);
// results.to_json(r'dateFrame_json.json', orient='split')
var deserializedData = JsonConvert.DeserializeObject<List<BacktestResult2>>(data);
return null;
}
JsonReaderand assign values by hand and the second one is to useDictionary<string, T>foreach entry on the root object.orientparameter. Which one are you using? Can you change it to something other JSON libraries can handle?roworcolumnorientation. Which one did you use? You don't need a JsonReader, although it could improve performance. You could useJObject.Parseto parse that JSON string and use eg LINQ to query it, but you still need to know if it's per row or per column