【问题标题】:Data from Api, showing multiple objects without array. should the class be created individually for each object来自 Api 的数据,显示多个没有数组的对象。是否应该为每个对象单独创建类
【发布时间】:2018-06-14 08:40:27
【问题描述】:

我有来自 api 的以下数据,它给了我以下数据。

我想访问每个日期下的单个数据,并使用 c# 将其放入 Unity 中的图表中。

当我检查示例时,我可以找到仅涉及数组的示例。但是这个 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"
    }
}}

【问题讨论】:

    标签: c# json unity3d


    【解决方案1】:

    好的。首先你的 json 很伤脑筋。如果你能解决它,那就去做吧:

    • 您的键中不应有索引。
    • Weekly Adjusted Time Series 应该是一个数组。
    • 日期不应是键,而应是对象内部的值。像这样的东西:{ "date": "2018-05-25", "open": "97.0000", [...] }

    如果您无法修复 json,这是一个解决方案,我不确定它是否很棒。我希望别人能提供更好的东西。我们开始:

    Demo online on .NetFiddle

    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);
    

    【讨论】:

      【解决方案2】:

      另一个答案的另一个帖子。自定义JsonConverter 是实现您想要的东西的一种不那么老套的方式。我们再次尝试与 JSON 作斗争。让我们看看:

      Demo on .NetFiddle

      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);
      

      【讨论】:

      • @aloistg。非常感谢您的支持..代码效果很好..我得到了我想要的..
      • @Krishna_psk 很高兴为您提供帮助!不要忘记将其标记为已解决并进行投票。在 Stack Overflow 上玩得开心?
      猜你喜欢
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 2019-11-13
      • 2021-11-20
      • 1970-01-01
      相关资源
      最近更新 更多