title: JSON to object C# (mapping complex API response to C# object)
body: <p>I am able to handle simple JSON serialization and deserialization but this API response seems little complicated, and I am seeking an advice as to what would be ideal approach to tackle this.</p>
  
  <p>I'm trying to call an API for MVC application.
  Goal is to map API data to model.
  API endpoint is 
  <a href="https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&amp;symbol=MSFT&amp;interval=1min&amp;apikey=MyAPIKey" rel="nofollow noreferrer">https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&amp;symbol=MSFT&amp;interval=1min&amp;apikey=MyAPIKey</a></p>
  
  <p>Troubles here are:</p>
  
  <ol>
  <li>JSON data keys have white space in them. </li>
  <li>When I tried doing paste special in Visual studio, It gave me a long
  list of classes for each date entry separately, because this API
  call returns a separate set of information for date.</li>
  </ol>
  
  <p>To solve problem explained in point 1, I used <code>[JsonProperty("1. Information")]</code> in class. And in my code..</p>
  
  <pre><code>        public async Task TSI()
          {
              HttpClient client = new HttpClient();
              //Uri uri = new Uri("http://date.jsontest.com/");
              Uri uri = new Uri("https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&amp;symbol=MSFT&amp;interval=5min&amp;apikey=demo");
              client.DefaultRequestHeaders.Accept.Clear();
              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
              HttpResponseMessage response = await client.GetAsync(uri);
              if (response.IsSuccessStatusCode)
              {
                  dynamic result = await response.Content.ReadAsAsync&lt;object&gt;();
  
                  IEnumerable&lt;dynamic&gt; dObj = JsonConvert.DeserializeObject&lt;dynamic&gt;(result.ToString());
  
                  IEnumerable&lt;dynamic&gt; t1 = dObj.FirstOrDefault();
                  IEnumerable&lt;dynamic&gt; t2 = dObj.LastOrDefault();
                  dynamic MetaData = t1.FirstOrDefault();
  
                  Rootobject ro = new Rootobject();
                  ro.MetaData = MetaData;
  
              }
  </code></pre>
  
  <p>PS: I'm relatively new to make API calls and handling them.</p>
  
  <p>I was able to make a call to </p>
  
  <blockquote>
    <p>date.jsontest.com</p>
  </blockquote>
  
  <p>and map the API data to model (which I had created using paste special)</p>
  
  <pre><code>//API response
      {
          "time": "12:53:22 PM",
          "milliseconds_since_epoch": 1504875202754,
          "date": "09-08-2017"
      }
  
  //C# code to map to API data
      public class sampleObject
      {
          public string time { get; set; }
          public long milliseconds_since_epoch { get; set; }
          public string date { get; set; }
      }
  </code></pre>
  
  <p>My RootObject looks like this:</p>
  
  <pre><code>public class Rootobject
      {
          [JsonProperty("Meta Data")]
          public MetaData MetaData { get; set; }
  
          [JsonProperty("Time Series (1min)")]
          public TimeSeries1Min TimeSeries1min { get; set; }
      }
  
      public class MetaData
      {
          [JsonProperty("1. Information")]
          public string _1Information { get; set; }
  
          [JsonProperty("2. Symbol")]
          public string _2Symbol { get; set; }
  
          [JsonProperty("3. Last Refreshed")]
          public string _3LastRefreshed { get; set; }
  
          [JsonProperty("4. Interval")]
          public string _4Interval { get; set; }
  
          [JsonProperty("5. Output Size")]
          public string _5OutputSize { get; set; }
  
          [JsonProperty("6. Time Zone")]
          public string _6TimeZone { get; set; }
      }
  
  // I have so many of these sub-classes for dates, which again is an issue
      public class TimeSeries1Min
      {
              public _20170907160000 _20170907160000 { get; set; }
              public _20170907155900 _20170907155900 { get; set; }
  ....
  ....}
  
  
  
  public class _20170907160000
      {
          public string _1open { get; set; }
          public string _2high { get; set; }
          public string _3low { get; set; }
          public string _4close { get; set; }
          public string _5volume { get; set; }
      }
  
      public class _20170907155900
      {
          public string _1open { get; set; }
          public string _2high { get; set; }
          public string _3low { get; set; }
          public string _4close { get; set; }
          public string _5volume { get; set; }
      }
  </code></pre>