I have a C# small console app that I need to run, it consumes an HTTP restful API. My problem is that using JSON I get a "Cannot implement type blahblah with a collection initializer because it does not implement 'System.Collections.IEnumerable' " and I'm lost. Coud you help me please? Posting the relevant part of the code..
using System;using System.Net.Http;using System.Net.Http.Headers;using Newtonsoft.Json;using System.Threading.Tasks;namespace TESTMEname{ public class headers { public string Request_ID { get; set; } = "XX123456789"; public string Correlation_ID { get; set; } = "12345678910"; public string Token { get; set; } = "abcdefghijklmnopqrstuvwxyzetc"; public string Content_Type { get; set; } = "application/x-www-form-urlencoded"; } public class access { public string allPs { get; set; } = "allAccounts"; public string availableAccounts { get; set; } = "allAccounts"; } public class Body { public access access { get; set; } public bool combinedServiceIndicator { get; set; } = false; public int frequencyPerDay { get; set; } = 4; public bool recurringIndicator { get; set; } = false; public string validUntil { get; set; } = "2020-12-31"; } [System.Serializable] public class Consent //RootObject { public headers headers { get; set; } public Body body { get; set; } }
try simple object
and add new "property" one by one until it fail
so first try to post a simple like this one :
{"headers":{"a":"something"}}
of course update the class so it match everytime you add a new property
N M
ASKER
Thank you
I tried, it partially works up to the point that I have the "access" with two nested properties.
Then, the JSON serialization fails..
With the above code, seems the only problem populating the properties is this error, hence I am trying to see what it means (I tried "by the book" example of Microsoft but did not work, and interestingly enough, I end up with same error..
// <auto-generated />//// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do://// using QuickType;//// var welcome = Welcome.FromJson(jsonString);namespace QuickType{ using System; using System.Collections.Generic; using System.Globalization; using Newtonsoft.Json; using Newtonsoft.Json.Converters; public partial class Welcome { [JsonProperty("headers")] public Headers Headers { get; set; } [JsonProperty("body")] public Body Body { get; set; } } public partial class Body { [JsonProperty("access")] public Access Access { get; set; } [JsonProperty("combinedServiceIndicator")] public bool CombinedServiceIndicator { get; set; } [JsonProperty("frequencyPerDay")] public long FrequencyPerDay { get; set; } [JsonProperty("recurringIndicator")] public bool RecurringIndicator { get; set; } [JsonProperty("validUntil")] public DateTimeOffset ValidUntil { get; set; } } public partial class Access { [JsonProperty("allPsd2")] public string AllPsd2 { get; set; } [JsonProperty("availableAccounts")] public string AvailableAccounts { get; set; } } public partial class Headers { [JsonProperty("X-Request-ID")] public string XRequestId { get; set; } [JsonProperty("LH-Correlation-ID")] public string LhCorrelationId { get; set; } [JsonProperty("LH-PSU-Token")] public string LhPsuToken { get; set; } [JsonProperty("Content-Type")] public string ContentType { get; set; } } public partial class Welcome { public static Welcome FromJson(string json) => JsonConvert.DeserializeObject<Welcome>(json, QuickType.Converter.Settings); } public static class Serialize { public static string ToJson(this Welcome self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings); } internal static class Converter { public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings { MetadataPropertyHandling = MetadataPropertyHandling.Ignore, DateParseHandling = DateParseHandling.None, Converters = { new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal } }, }; }}
(Note: I understand the effort on the date property, thing is, apart from integers and boolean values, all rest are strings, so, no need for extra effort on converting dates etc, is all same for me)
Thank you also for the suggestion on the quicktype. Is very interesting. However, my problem seems to be exactly at the point where I assign the values... I am trying now and will post a sample *if it compiles...
Open in new window
Hope it helps..