Im writing a c# console app, where i need get som JSON date from a webapi. For the most part this works fine, however in one of my JSON responses i get a property name staring with @. I cant seem to figure out how to put that JSON property into a C# object.
My code look as follows:
public class AlertResponse
{
public string @class { get; set; }
public string result { get; set; }
public string info { get; set; }
}
public class AuthenticationResponse
{
public string Access_token { get; set; }
}
class Test
{
private static HttpClient client = new HttpClient();
private static string BaseURL = "https://xxxxx.xxxx";
public void Run()
{
client.BaseAddress = new Uri(BaseURL);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
AuthenticationResponse authenticationResponse = Login();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResponse.Access_token);
AlertResponse OpenAlerts = GetOpenAlerts();
}
internal AlertResponse GetOpenAlerts()
{
var response = client.GetAsync("/api/v2/xxxxxxxxxxxxxxxxxx/alerts/open").Result;
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsAsync<AlertResponse>().Result;
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
return null;
}
private AuthenticationResponse Login()
{
string apiKey = "gdfashsfgjhsgfj";
string apiSecretKey = "sfhsfdjhssdjhsfhsfh";
var byteArray = new UTF8Encoding().GetBytes("public-client:public");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var form = new FormUrlEncodedContent(new Dictionary<string, string> { { "username", apiKey }, { "password", apiSecretKey }, { "grant_type", "password" } });
HttpResponseMessage tokenMessage = client.PostAsync("/auth/oauth/token", form).Result;
if (tokenMessage.IsSuccessStatusCode)
{
return tokenMessage.Content.ReadAsAsync<AuthenticationResponse>().Result;
}
else
{
Console.WriteLine("{0} ({1})", (int)tokenMessage.StatusCode, tokenMessage.ReasonPhrase);
}
return null;
}
}
And the JSON i get looks like this:
"AlertResponse": {
"@class": "patch_ctx",
"patchUid": "afdhgfhjdajafjajadfjadfjdj",
"policyUid": "dsgafdhasfjafhdafhadfh",
"result": "0x80240022",
"info": "fdgdfhsfgjh"
}
How can i fix this?
Best regards Glacier
CodePudding user response:
Are you using Newtonsoft.Json or System.Text.Json?
In either cases you should decorate the @class Property with
//System.Text.Json
[JsonPropertyName("@class")]
public string class { get; set; }
or
//Newtonsoft.Json
[JsonProperty("@class")]
public string class { get; set; }
CodePudding user response:
I guess you are looking for DataMemberAttribute and DataContract
using System.Runtime.Serialization;
[DataContract]
public class AlertResponse
{
[DataMember(Name = "@class")]
public string Class { get; set; }
[DataMember(Name = "result")]
public string Result { get; set; }
[DataMember(Name = "info")]
public string Info { get; set; }
}