I don't think the marked duplication is correct as I don't have any sort of root key to hang off, the duplicate example given had the static key "devices" where as my example doesn't have that.
How can you use JSON.Net to deserialize a part of a json when the key is dynamic, so for example
{"USD_GBP":{"val":0.777803}}
Or
{"USD_EUR":{"val":0.863705}}
My nonsense attempt looks like this (cut down version):
public class DeserializeFC
{
public Dictionary<string, double> jsonName { get; set; }
}
public void run()
{
DataTable outputData = new DataTable();
outputData.Columns.Add("timestamp", typeof(System.String));
outputData.Columns.Add("basecurrency", typeof(System.String));
outputData.Columns.Add("currency", typeof(System.String));
outputData.Columns.Add("rate", typeof(System.Decimal));
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
var response = httpClient.GetStringAsync(new Uri(fullURL)).Result;
string jsonName = loadBaseCurr + "_" + loadCurrency;
var result3 = JsonConvert.DeserializeObject<DeserializeFC>(response);
foreach (KeyValuePair<string, double> entry3 in result3.jsonName)
{
outputData.Rows.Add(DateTime.Now, loadBaseCurr, loadCurrency, entry3.Value);
}
}
}
This is a followup to this question: Deserialize part of json to datatable in C# using JSON.NET
The code which worked for me in the end was:
string jsonName = loadBaseCurr + "_" + loadCurrency;
var result3 = JObject.Parse(response);
var nested = result3[jsonName].ToObject<Dictionary<string, decimal>>();
foreach (var entry3 in nested)
{
outputData.Rows.Add(DateTime.Now, loadBaseCurr, loadCurrency, entry3.Value);
}