0

I am trying to get the public holidays for my uwp application. I used this code

var client = new HttpClient();
      var request = new HttpRequestMessage
      {
          Method = HttpMethod.Get,
          RequestUri = new Uri("https://public-holiday.p.rapidapi.com/2023/SG"),
          Headers =
          {
              { "X-RapidAPI-Key", "API-Key" },
              { "X-RapidAPI-Host", "public-holiday.p.rapidapi.com" },
          },
      };
      using (var response = await client.SendAsync(request))
      {
          response.EnsureSuccessStatusCode();
          var body = await response.Content.ReadAsStringAsync();
          var serializer = new DataContractJsonSerializer(typeof(RootObj));
          Debug.WriteLine(body);
          var ms = new MemoryStream(Encoding.UTF8.GetBytes(body));
          var data = (RootObj)serializer.ReadObject(ms);
          return data;
      }

I did get the Json respond when I put debug.WriteLine(body) but when I try to use this code in the MainPage.xaml.cs,

RootObj myHolidays = await GetHolidays();
holidayTextBlock.Text = myHolidays.name + " - " + myHolidays.date + " - " + myHolidays.type;

RootObj:

        public class RootObj
        {
            [DataMember]
            public string date { get; set; }
            [DataMember]
            public string localName { get; set; }
            [DataMember]
            public string name { get; set; }
            [DataMember]
            public string countryCode { get; set; }
            [DataMember]
            public bool @fixed { get; set; }
            [DataMember]
            public bool global { get; set; }
            [DataMember]
            public object counties { get; set; }
            [DataMember]
            public object launchYear { get; set; }
            [DataMember]
            public string type { get; set; }
        }

I get null for the name, date and type even though I tested the end point it showed something as well as it showed in the debug print statement. Any help would be appreciated. Thanks.

Kim
  • 11
  • 4
  • Have you checked the return value of `MemoryStream` and `serializer.ReadObject`? – Junjie Zhu - MSFT Aug 04 '23 at 08:31
  • What does your RootObj look like? – emoacht Aug 04 '23 at 18:36
  • @JunjieZhu-MSFT It shows Project.Public_Holidays+RootObj when I used debug.writeline(data) and System.IO.MemoryStream when I used debug.writeline(ms) – Kim Aug 05 '23 at 07:13
  • @emoacht I have added the RootObj above – Kim Aug 05 '23 at 07:18
  • Check the return value of that API. If the API which you accessed is https://rapidapi.com/theapiguy/api/public-holiday, the type of return value is not RootObj but the array of RootObj. – emoacht Aug 05 '23 at 08:06
  • @emoacht Oh it is an array I see. But how do I get that array to show the values? – Kim Aug 05 '23 at 08:28

1 Answers1

0

Check the type of return value of that API. If it is an array of a class, specify [class name][] as the type for DataContractJsonSerializer. Don't forget to insert using before MemoryStream to ensure it would be disposed.

var serializer = new DataContractJsonSerializer(typeof(RootObj[]));
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(body));
var data = (RootObj[])serializer.ReadObject(ms);
emoacht
  • 2,764
  • 1
  • 13
  • 24
  • I tried to run and I got this exception: System.InvalidCastException: 'Unable to cast object of type 'RootObj' to type 'RootObj[]'.' Edit: Ok I found out that I forgot to change something and now it works! Thank you for the help! – Kim Aug 05 '23 at 09:03
  • Did you set `typeof(RootObj[])` for DataContractJsonSerializer? – emoacht Aug 05 '23 at 09:08
  • At first, I didn't but then I realized that I have to do it for all RootObj so I changed it and it worked. Thanks again for the help, I was really clueless how to get the data I need from that api before this. – Kim Aug 05 '23 at 09:14