我认为标记的重复项是正确的,因为我没有任何根键可以挂起,给出的重复示例具有静态密钥“ devices”,而我的示例中没有。 / p>
当密钥为动态时,如何使用JSON.Net反序列化json的一部分,例如
{"USD_GBP":{"val":0.777803}}
或
{"USD_EUR":{"val":0.863705}}
我的废话尝试看起来像这样(简化版本):
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);
}
}
}
这是此问题的后续措施: Deserialize part of json to datatable in C# using JSON.NET
最后对我有用的代码是:
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);
}