我从api获得以下数据,它给出了以下数据。
我想访问每个日期下的个人数据,并将其放入Unity中的c#图表中。
当我检查示例时,我可以找到仅涉及数组的示例。但这个api仅作为单个对象投掷。现在我应该根据个人日期创建个别课程吗?我有近250个日期,我需要从中提取数据。
{
"Meta Data": {
"1. Information": "Weekly Adjusted Prices and Volumes",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2018-06-13",
"4. Time Zone": "US/Eastern"
},
"Weekly Adjusted Time Series": {
"2018-06-13": {
"1. open": "101.3700",
"2. high": "102.0100",
"3. low": "100.5600",
"4. close": "100.8500",
"5. adjusted close": "100.8500",
"6. volume": "70511616",
"7. dividend amount": "0.0000"
},
"2018-06-08": {
"1. open": "101.2600",
"2. high": "102.6900",
"3. low": "100.3800",
"4. close": "101.6300",
"5. adjusted close": "101.6300",
"6. volume": "122316267",
"7. dividend amount": "0.0000"
},
"2018-06-01": {
"1. open": "97.8400",
"2. high": "100.8600",
"3. low": "97.2300",
"4. close": "100.7900",
"5. adjusted close": "100.7900",
"6. volume": "113626024",
"7. dividend amount": "0.0000"
},
"2018-05-25": {
"1. open": "97.0000",
"2. high": "98.9800",
"3. low": "96.3200",
"4. close": "98.3600",
"5. adjusted close": "98.3600",
"6. volume": "101128083",
"7. dividend amount": "0.0000"
}
}}
答案 0 :(得分:0)
确定。首先你的json是大脑受伤了。如果你能解决它,那就去做吧:
Weekly Adjusted Time Series
应该是一个数组。{ "date": "2018-05-25", "open": "97.0000", [...] }
嗯 IF 你无法修复json,这是一个解决方案,我不确定它是一个伟大的。我希望别人能提供更好的东西。我们走了:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters = {
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
public class Welcome
{
[JsonProperty("Meta Data")]
public MetaData MetaData { get; set; }
// we save it for a second deserialize
[JsonProperty("Weekly Adjusted Time Series")]
private JObject JWeeklyAdjustedTimeSeries { get; set; }
// this is the object as it should be
public IEnumerable<WeeklyAdjustedTime> WeeklyAdjustedTimeSeries { get; set; }
public static Welcome FromJson(string json)
{
// first pass
var welcome = JsonConvert.DeserializeObject<Welcome>(json, Converter.Settings);
// second pass
welcome.WeeklyAdjustedTimeSeries = welcome.JWeeklyAdjustedTimeSeries.ToObject<Dictionary<string, JObject>>().Select(x => {
var wat = x.Value.ToObject<WeeklyAdjustedTime>();
return new WeeklyAdjustedTime
{
Date = x.Key,
Open = wat.Open,
High = wat.High,
Low = wat.Low,
Close = wat.Close,
AdjustedClose = wat.AdjustedClose,
Volume = wat.Volume,
DividendAmount = wat.DividendAmount
};
});
return welcome;
}
}
public class MetaData
{
[JsonProperty("1. Information")]
public string Information { get; set; }
[JsonProperty("2. Symbol")]
public string Symbol { get; set; }
[JsonProperty("3. Last Refreshed")]
public DateTimeOffset LastRefreshed { get; set; }
[JsonProperty("4. Time Zone")]
public string TimeZone { get; set; }
}
public class WeeklyAdjustedTime
{
public string Date { get; set; }
[JsonProperty("1. open")]
public string Open { get; set; }
[JsonProperty("2. high")]
public string High { get; set; }
[JsonProperty("3. low")]
public string Low { get; set; }
[JsonProperty("4. close")]
public string Close { get; set; }
[JsonProperty("5. adjusted close")]
public string AdjustedClose { get; set; }
[JsonProperty("6. volume")]
public string Volume { get; set; }
[JsonProperty("7. dividend amount")]
public string DividendAmount { get; set; }
}
如何使用它:
var welcome = Welcome.FromJson(json); // put your json here
Console.WriteLine(welcome.MetaData.Information);
Console.WriteLine(welcome.WeeklyAdjustedTimeSeries.First().Date);
Console.WriteLine(welcome.WeeklyAdjustedTimeSeries.First().Open);
答案 1 :(得分:0)
另一个答案的另一篇文章。自定义JsonConverter
是一种不那么愚蠢的方式来实现您想要的。我们再次尝试与JSON作斗争。让我们看看:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters = {
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
}
};
}
public class Welcome
{
[JsonProperty("Meta Data")]
public MetaData MetaData { get; set; }
// Look Ma! No hands!
[JsonProperty("Weekly Adjusted Time Series")]
[JsonConverter(typeof(WelcomeConverter))]
public IEnumerable<WeeklyAdjustedTime> WeeklyAdjustedTimeSeries { get; set; }
}
public class WelcomeConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(object));
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException("Not implemented yet");
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
{
return Enumerable.Empty<WeeklyAdjustedTime>();
}
// first and only pass!
return JObject.Load(reader).ToObject<Dictionary<string, JObject>>().Select(x => {
var wat = x.Value.ToObject<WeeklyAdjustedTime>();
return new WeeklyAdjustedTime
{
Date = x.Key,
Open = wat.Open,
High = wat.High,
Low = wat.Low,
Close = wat.Close,
AdjustedClose = wat.AdjustedClose,
Volume = wat.Volume,
DividendAmount = wat.DividendAmount
};
});
}
}
public class MetaData
{
[JsonProperty("1. Information")]
public string Information { get; set; }
[JsonProperty("2. Symbol")]
public string Symbol { get; set; }
[JsonProperty("3. Last Refreshed")]
public DateTimeOffset LastRefreshed { get; set; }
[JsonProperty("4. Time Zone")]
public string TimeZone { get; set; }
}
public class WeeklyAdjustedTime
{
public string Date { get; set; }
[JsonProperty("1. open")]
public string Open { get; set; }
[JsonProperty("2. high")]
public string High { get; set; }
[JsonProperty("3. low")]
public string Low { get; set; }
[JsonProperty("4. close")]
public string Close { get; set; }
[JsonProperty("5. adjusted close")]
public string AdjustedClose { get; set; }
[JsonProperty("6. volume")]
public string Volume { get; set; }
[JsonProperty("7. dividend amount")]
public string DividendAmount { get; set; }
}
如何使用它:
var welcome = JsonConvert.DeserializeObject<Welcome>(json, Converter.Settings); // put your json here
Console.WriteLine(welcome.MetaData.Information);
Console.WriteLine(welcome.WeeklyAdjustedTimeSeries.First().Date);
Console.WriteLine(welcome.WeeklyAdjustedTimeSeries.First().Open);