1

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.

I'm trying to call an API for MVC application. Goal is to map API data to model. API endpoint is https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=MyAPIKey

Troubles here are:

  1. JSON data keys have white space in them.
  2. 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.

To solve problem explained in point 1, I used [JsonProperty("1. Information")] in class. And in my 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&symbol=MSFT&interval=5min&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<object>();

                IEnumerable<dynamic> dObj = JsonConvert.DeserializeObject<dynamic>(result.ToString());

                IEnumerable<dynamic> t1 = dObj.FirstOrDefault();
                IEnumerable<dynamic> t2 = dObj.LastOrDefault();
                dynamic MetaData = t1.FirstOrDefault();

                Rootobject ro = new Rootobject();
                ro.MetaData = MetaData;

            }

PS: I'm relatively new to make API calls and handling them.

I was able to make a call to

date.jsontest.com

and map the API data to model (which I had created using paste special)

//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; }
    }

My RootObject looks like this:

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; }
    }
7
  • I understand that, I'm sorry for this all confusions and complexity of the question. And answers I have got so far have made me progress a little. Regarding JSON parsing, I was able to serialize and deserialize simple JSON data. But this one is way too complex and I was wondering if any one has good approach to this. I might close this question soon :-) Commented Sep 8, 2017 at 14:09
  • @MatthiasBurger json2charp produces garbage (as expected in this case).. Commented Sep 8, 2017 at 14:13
  • @L.B. only if you are a copy-paste-developer :) (sorry, mean no harm) for sure, you need to check and compare, but json2csharp does its job. it's the job of a developer to check if it fits. :) Commented Sep 8, 2017 at 14:15
  • 1
    @MatthiasBurger have you seen the model it generates for the json in question? It can not be modelled like this.Also read my answers related with json to see when I use that site and when not. stackoverflow.com/search?q=user:932418+[json] Commented Sep 8, 2017 at 14:16
  • 1
    @Matt.. thanks for taking negative vote back, I'm trying hard to reach 15 reputations :P Commented Sep 8, 2017 at 23:39

2 Answers 2

6

It is hard to create a model from this json, but you can convert those data to dictionary

var jObj = JObject.Parse(json);
var metadata = jObj["Meta Data"].ToObject<Dictionary<string, string>>();
var timeseries = jObj["Time Series (1min)"].ToObject<Dictionary<string, Dictionary<string, string>>>();
Sign up to request clarification or add additional context in comments.

5 Comments

This seem to be a good solution. Meta Data worked just fine, but the second one didn't give expected result. And tbh, I wasn't even aware of this approach. I think the response is way too complicated to handle.
but the second one didn't give expected result what did you expect, what did you get? It gives 100 dictionaries in one dict (i.e each Value is another dictionary).
The first one gives me expected dictionary as soon it is processed (I have set breakpoint that line). but the execution fails as soon as second line is hit.
@JigarPatel I tested above code with your json before posting...What error do you get?
Ah! Silly mistake I was making, the Uri I had in my visual studio has interval set to 5 minutes, and the one I have posted here is 1 minute one. I think I better go to bed now and let my mind rest. Thank you for the answer.
1

The following code should do what you want

       if (response.IsSuccessStatusCode)
        {
            var result = await response.Content.ReadAsStringAsync();

            var obj = JsonConvert.DeserializeObject<Rootobject>(result);
            //No idea what you want to do with this line as there is no MetaData property on the root object
            obj.MetaData = MetaData;

        }

2 Comments

I'm sorry for the confusion. I will edit a question in a minute. I have updated the question.
Hey ObiEff, your answer has helped me too. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.