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
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; }
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; }
}
Related
I have the following bit of code whihc sends a Http POST request to the server. The server reurns a 400 Bad request response along with a error object in the form of Json:
namespace MyApp.Shared.Dtos.Response
{
public class ErrorItem
{
public string Message { get; set; }
public string Tag { get; set; }
}
public class ErrorDto
{
public string Title { get; set; }
public List<ErrorItem> Errors { get; set; } = new();
}
}
namespace Accounting.Web.Services
{
public interface IHttpService
{
Task<T> Get<T>(string uri);
Task<T> Post<T>(string uri, object value, bool addBearerToken = false);
public ErrorDto Error { get; set; }
}
public class HttpService: IHttpService
{
private HttpClient _httpClient;
public ErrorDto Error { get; set; }
public HttpService(HttpClient httpClient)
{
_httpClient = httpClient;
_stateService = stateService;
}
public async Task<T> Post<T>(string uri, object value)
{
var request = new HttpRequestMessage(HttpMethod.Post, uri);
request.Content = new StringContent(JsonSerializer.Serialize(value), Encoding.UTF8, "application/json");
return await sendRequest<T>(request, addBearerToken);
}
private async Task<T> sendRequest<T>(HttpRequestMessage request)
{
using var response = await _httpClient.SendAsync(request);
if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
{
var result = await response.Content.ReadAsStringAsync();
Error = JsonSerializer.Deserialize<ErrorDto>(result);
//..
}
else
{
//..
}
}
}
}
The result correctly recieves the following response from the server as a JSON string:
{"title":"Username or password is incorrect","errors":[]}
And I can confirm by inspecting var result, it has the above value.
However, It doesn't seem deserialize into the ErrorDto class as one would expect it to:
Error = JsonSerializer.Deserialize(result);
But I simply cannot see any problems with the code, it looks like it should be working.
*** UPDATE ***
My server API code returrns the JSOn using the same DTO class (It's a shared class) using the following code:
[HttpPost("authenticate")]
public ActionResult Authenticate(AuthenticateRequest loginRequest)
{
var auth = _userService.Authenticate(loginRequest);
ErrorDto error = new()
{
Title = "Username or password is incorrect"
};
if (auth.user == null || auth.token == null)
{
return BadRequest(error);
}
return Ok(auth.user.ConvertToDto(auth.token));
}
By default System.Text.Json is case-sensitive. There are multiple options to handle this, for example by providing corresponding JsonSerializerOptions:
var json = #"{""title"":""Username or password is incorrect"",""errors"":[]}";
var errorDto = JsonSerializer.Deserialize<ErrorDto>(json, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
Or marking properties with corresponding JsonPropertyNameAttribute:
public class ErrorItem
{
[JsonPropertyName("message")]
public string Message { get; set; }
[JsonPropertyName("tag")]
public string Tag { get; set; }
}
public class ErrorDto
{
[JsonPropertyName("title")]
public string Title { get; set; }
[JsonPropertyName("errors")]
public List<ErrorItem> Errors { get; set; } = new();
}
UPD
From How to customize property names and values with System.Text.Json doc:
Note
The web default is camel case.
If you want to switch from camel case to the naming policy used for DTOs you can do the following:
builder.Services.AddControllers()
.AddJsonOptions(opts => opts.JsonSerializerOptions.PropertyNamingPolicy = null);
I want to return C# class object instead of using JObject in here. Could someone can tell me how to use it.
private async Task<JObject> GetReceiptById(string Id, string name)
{
var response = await _ApiService.Get(Id, name);
var responseStr = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
return JObject.Parse(responseStr);
}
throw new Exception(responseStr);
}
this method is return (return JObject.Parse(responseStr)); below JSON output. for that how to create a new class. I am not sure how to apply all in one class.
{
"receipts": [
{
"ReceiptHeader": {
"Company": "DHSC",
"ErpOrderNum": "730",
"DateTimeStamp": "2022-05-14T13:43:57.017"
},
"ReceiptDetail": [
{
"Line": 1.0,
"ITEM": "PP1016",
"ITEM_NET_PRICE": 0.0
},
{
"Line": 2.0,
"ITEM": "PP1016",
"ITEM_NET_PRICE": 0.0
}
],
"XrefItemsMapping": [],
"ReceiptContainer": [],
"ReceiptChildContainer": [],
"rPrefDO": {
"Active": null,
"AllowLocationOverride": null,
"DateTimeStamp": null
}
}
]
}
You can bind the Response Content to a known Type using ReadAsAsync<T>().
https://learn.microsoft.com/en-us/previous-versions/aspnet/hh835763(v=vs.118)
var result = await response.Content.ReadAsAsync<T>();
In your example you will also run into further issues as you are not closing your response after getting it from the Api Service Get method.
Below is a possible solution where you send your object type to the Get method. (not tested)
public virtual async Task<T> GetApiCall<T>(Id, name)
{
//create HttpClient to access the API
var httpClient = NewHttpClient();
//clear accept headers
httpClient.DefaultRequestHeaders.Accept.Clear();
//add accept json
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//return the client for use
using (var client = await httpClient )
{
//create the response
HttpResponseMessage response = await client.GetAsync(url);
if (response.IsSuccessStatusCode)
{
//create return object
try
{
var result = await response.Content.ReadAsAsync<T>();
//dispose of the response
response.Dispose();
return result;
}
catch
{
throw;
}
}
// do something here when the response fails for example
var error = await response.Content.ReadAsStringAsync();
//dispose of the response
response.Dispose();
throw new Exception(error);
}
}
What you probably looking for is Deserialization
you can achieve it with
var model = JsonConvert.Deserialize<YourClass>(responseStr);
return model;
but the class (YourClass) properties must match the json string you provided in responseStr.
As the comments section you asked for a generated class:
here is what should look like, after you generate the class.
Note: most of the times, you will need to edit the generated class.
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Receipt
{
public ReceiptHeader ReceiptHeader { get; set; }
public List<ReceiptDetail> ReceiptDetail { get; set; }
public List<object> XrefItemsMapping { get; set; }
public List<object> ReceiptContainer { get; set; }
public List<object> ReceiptChildContainer { get; set; }
public RPrefDO rPrefDO { get; set; }
}
public class ReceiptDetail
{
public double Line { get; set; }
public string ITEM { get; set; }
public double ITEM_NET_PRICE { get; set; }
}
public class ReceiptHeader
{
public string Company { get; set; }
public string ErpOrderNum { get; set; }
public DateTime DateTimeStamp { get; set; }
}
public class Root
{
public List<Receipt> receipts { get; set; }
}
public class RPrefDO
{
public object Active { get; set; }
public object AllowLocationOverride { get; set; }
public object DateTimeStamp { get; set; }
}
generated by: https://json2csharp.com/
Well, I'm new to programming, and I have a problem.
This is my class Valores
public class Valores
{
[JsonProperty("nome")]
public string Nome { get; set; }
[JsonProperty("valor")]
public double Valor { get; set; }
[JsonProperty("ultima_consulta")]
public int UltimaConsulta { get; set; }
[JsonProperty("fonte")]
public string Fonte { get; set; }
}
And this is my method where I get and deserialize my Json
public static async Task<Valores> GetAsync()
{
Valores valores = null;
using (var client = new HttpClient())
{
var json = await client.GetStringAsync("http://api.promasters.net.br/cotacao/v1/valores");
valores = JsonConvert.DeserializeObject<Valores>(json);
}
return valores;
}
This is json that the way: "http://api.promasters.net.br/cotacao/v1/valores" returns.
{
"status": true,
"valores": {
"USD": {
"nome": "Dólar",
"valor": 3.0717,
"ultima_consulta": 1490040302,
"fonte": "UOL Economia - http://economia.uol.com.br/"
},
"EUR": {
"nome": "Euro",
"valor": 3.3002,
"ultima_consulta": 1490040302,
"fonte": "UOL Economia - http://economia.uol.com.br/"
},
"ARS": {
"nome": "Peso Argentino",
"valor": 0.1965,
"ultima_consulta": 1490040302,
"fonte": "UOL Economia - http://economia.uol.com.br/"
},
"GBP": {
"nome": "Libra Esterlina",
"valor": 3.7966,
"ultima_consulta": 1490040302,
"fonte": "UOL Economia - http://economia.uol.com.br/"
},
"BTC": {
"nome": "Bitcoin",
"valor": 3472,
"ultima_consulta": 1490067603,
"fonte": "Mercado Bitcoin - http://www.mercadobitcoin.com.br/"
}
}
}
I do not know what I did wrong, because this
var json = await client.GetStringAsync("http://api.promasters.net.br/cotacao/v1/valores");
It was to receive Json in string, but is not receiving anything, it's like an empty string.
I experimented a bit, and it appears the web site requires the user agent request string to be set to something in order for JSON to be returned. And, by something, I mean that the string "something" seems to work, as does the string "Wget/1.11.4". You should check the documentation (Portugese) to make sure there are no restrictions on programmatic access to this site, and comply with those access restrictions (if any).
Also, your data model does not reflect your JSON. You need a higher level root object as follows:
public class RootObject
{
public RootObject() { this.valores = new Dictionary<string, Valores>(); }
public bool status { get; set; }
public Dictionary<string, Valores> valores { get; set; }
}
Here is a sample fiddle that successfully downloads and deserializes JSON from the site by setting the user agent. It uses WebClient since that's what seems to be available on https://dotnetfiddle.net/:
public static async Task<RootObject> GetAsync()
{
using (var client = new WebClient())
{
client.Headers["User-Agent"] = "something";
var json = await client.DownloadStringTaskAsync(#"http://api.promasters.net.br/cotacao/v1/valores");
var root = JsonConvert.DeserializeObject<RootObject>(json);
return root;
}
}
And for HttpClient I think it should be (untested):
public static async Task<RootObject> GetAsync()
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", "something");
var json = await client.GetStringAsync("http://api.promasters.net.br/cotacao/v1/valores");
return JsonConvert.DeserializeObject<RootObject>(json);
}
See also this post for a discussion of whether to dispose the HttpClient.
There issue here is that the deserialised json is not correctly getting mapped to a an object. Try to create a wrapper class over your model (Valores) and then try to map.
public class Valores
{
[JsonProperty("nome")]
public string Nome { get; set; }
[JsonProperty("valor")]
public double Valor { get; set; }
[JsonProperty("ultima_consulta")]
public int UltimaConsulta { get; set; }
[JsonProperty("fonte")]
public string Fonte { get; set; }
}
public class ValoresList
{
public boolean status { get; set; }
public Valores USD { get; set; }
public Valores EUR { get; set; }
public Valores ARS { get; set; }
public Valores GBP { get; set; }
public Valores BTC { get; set; }
}
And then map the wrapper class to the deserialised json.
public static async Task<ValoresList> GetAsync()
{
ValoresList valores = null;
using (var client = new HttpClient())
{
var json = await client.GetStringAsync("http://api.promasters.net.br/cotacao/v1/valores");
valores = JsonConvert.DeserializeObject<ValoresList>(json);
}
return valores;
}
The problem is the json returned contains a collection of Valores in a dictionary, you're trying to map the response to a single Valore object.
Your container wants to look like this. The key for the dictionary is the string representation of the currency.
class Container
{
public bool Status { get; set; }
public Dictionary<string, ValorInfo> Valores { get; set; }
}
Your Valor class is good as is, I renamed it to prevent a conflict on the class name to Valor property.
class ValorInfo
{
[JsonProperty("nome")]
public string Nome { get; set; }
[JsonProperty("valor")]
public double Valor { get; set; }
[JsonProperty("ultima_consulta")]
public int UltimaConsulta { get; set; }
[JsonProperty("fonte")]
public string Fonte { get; set; }
}
Finally this is how you use it, props to #dbc for spotting the user-agent requirement, that had me stumped!
async void Main()
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("User-Agent", "linqpad");
var response = await client.GetAsync("http://api.promasters.net.br/cotacao/v1/valores");
response.EnsureSuccessStatusCode();
var container = await response.Content.ReadAsAsync<Container>();
// do stuff with container...
}
}
I am developing Web API for my client. They have suggestion that all response should be a common JSON structure.
{ Data:"", Status:true, Message:"" }
If error means
{ Error:"", Status:false, Message:"" }
Which is the best method to create a common JSON structure as returns.
Now I created a class having these properties. And created 2 classes from IHttpActionResult,Error.cs and Success.cs, From that the response is created and returned from the controller.
The thing is in my controller,
public IHttpActionResult GetNewsAndAnnouncements()
{
var data = newsAndAnnouncementsDataServices.NewsAndAnnouncements();
if (data != null && data.Count() > 0)
{
return new Success(Request, "News and Announcements Retrieved Successfully", data);
}
return new Error(Request, "No News and Announcements Found");
}
Error.cs
public class Error : IHttpActionResult
{
private readonly string _message;
private readonly HttpRequestMessage _request;
private IErrorResponseModel errorResponse;
public Error(HttpRequestMessage request, string message)
{
_message = message;
_request = request;
errorResponse = new ErrorResponseModel();
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
errorResponse.Message = _message;
errorResponse.Status = false;
errorResponse.Error = _message;
var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new ObjectContent<object>(errorResponse, new JsonMediaTypeFormatter()),
RequestMessage = _request
};
return Task.FromResult(response);
}
}
Success.cs
public class Success : IHttpActionResult
{
private readonly string _message;
private readonly object _data;
private readonly HttpRequestMessage _request;
private IDataResponseModel dataResponse = new DataResponseModel();
public Success(HttpRequestMessage request, string message, object data)
{
_message = message;
_request = request;
_data = data;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
dataResponse.Message = _message;
dataResponse.Status = true;
dataResponse.Data = _data;
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ObjectContent<object>(dataResponse, new JsonMediaTypeFormatter()),
RequestMessage = _request
};
return Task.FromResult(response);
}
}
DataResponseModel.cs
public class DataResponseModel : Mobility.Common.IDataResponseModel
{
public object Data { get; set; }
public string Message { get; set; }
public bool Status { get; set; }
}
ErrorResponseModel.cs
public class ErrorResponseModel : Mobility.Common.IErrorResponseModel
{
public object Error { get; set; }
public string Message { get; set; }
public bool Status { get; set; }
}
Is this a right method. I need suggestion. Is there any other way to achieve this. I heard about delegating handler something. But I don't have much idea on these.
Please help me.
Another solution to this problem is to hook into the ASP.NET pipeline using custom handlers to build a common response object.
For instance:
[DataContract]
public class ApiResponse
{
[DataMember]
public string Version { get { return "1.2.3"; } }
[DataMember]
public int StatusCode { get; set; }
[DataMember(EmitDefaultValue = false)]
public string ErrorMessage { get; set; }
[DataMember(EmitDefaultValue = false)]
public object Result { get; set; }
public ApiResponse(HttpStatusCode statusCode, object result = null, string errorMessage = null)
{
StatusCode = (int)statusCode;
Result = result;
ErrorMessage = errorMessage;
}
}
Have a look at this post for a reference implementation http://www.devtrends.co.uk/blog/wrapping-asp.net-web-api-responses-for-consistency-and-to-provide-additional-information
"responseCode": String
"responseMessage": String
"responseBody": { "conversations": [
{
"conversationId": String,
"state": String,
"conversationType": String,
"mediaType": Enum,
"startDate":Integer,
"duration": Integer,
"tags":[{ "tagName":String,
"tagType":String,
"tagCreateDate":Integer,
"tagOffset":Integer
}],
]}
This schema continues, but my question regarding the first section applies to the rest...
How can I deserialize a JSON response based on this schema into .NET objects? what would the .NET object look like?
Is there another way to read it ? (like a .NET Dataset type of way?)
Thanks. Roey.
If you want (or have to) to use JavaScriptSerializer the code could look like following:
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
namespace JsonSer {
public class MyTag {
public string tagName { get; set; }
public string tagType { get; set; }
public long tagCreateDate { get; set; }
public int tagOffset { get; set; }
}
public enum MyMedia {
Diskette,
UsbStick,
Disk,
Internet
}
public class MyConversation {
public string conversationId { get; set; }
public string state { get; set; }
public string conversationType { get; set; }
public MyMedia mediaType { get; set; }
public long startDate { get; set; }
public int duration { get; set; }
public List<MyTag> tags { get; set; }
}
public class MyConversations {
public List<MyConversation> conversations { get; set; }
}
public class MyData {
public string responseCode { get; set; }
public string responseMessage { get; set; }
public MyConversations responseBody { get; set; }
}
class Program {
static void Main (string[] args) {
MyData data = new MyData () {
responseCode = "200",
responseMessage = "OK",
responseBody = new MyConversations () {
conversations = new List<MyConversation> () {
new MyConversation() {
conversationId = "conversation1",
state = "state1",
conversationType = "per JSON",
mediaType = MyMedia.Internet,
startDate = DateTime.Now.Ticks,
duration = 12345,
tags = new List<MyTag>() {
new MyTag() {
tagName = "tagName1",
tagType = "tagType1",
tagCreateDate = DateTime.Now.Ticks,
tagOffset = 1
}
}
}
}
}
};
Console.WriteLine ("The original data has responseCode={0}", data.responseMessage);
JavaScriptSerializer serializer = new JavaScriptSerializer ();
string json = serializer.Serialize (data);
Console.WriteLine ("Data serialized with respect of JavaScriptSerializer:");
Console.WriteLine (json);
MyData d = (MyData)serializer.Deserialize<MyData> (json);
Console.WriteLine ("After deserialization responseCode={0}", d.responseMessage);
}
}
}
the corresponding JSON data will be look like
{
"responseCode": "200",
"responseMessage": "OK",
"responseBody": {
"conversations": [
{
"conversationId": "conversation1",
"state": "state1",
"conversationType": "per JSON",
"mediaType": 3,
"startDate": 634207605160873419,
"duration": 12345,
"tags": [
{
"tagName": "tagName1",
"tagType": "tagType1",
"tagCreateDate": 634207605160883420,
"tagOffset": 1
}
]
}
]
}
}
You can easy modify the code if you decide to use DataContractJsonSerializer.
First you can beautify all your JSON using http://jsbeautifier.org/ to make it more readable, and then the only way I know is to just go through every property step by step and create classes for them. You should add the [DataContract] attribute for classes and the [DataMember] attribute for properties.
Example
[DataContract]
public class Response{
[DataMember]
public string responseCode {get;set;}
[DataMember]
public string responseMessage {get;set;}
[DataMember]
public ResponseBody responseBody {get;set;}
}
Automatic generation of these classes
There are alternatives for XMLSerialization (using XSD) but as far as I know there are no similar solutions for json thus far.
To finally deserialize the json into .NET object you can use the following code:
Response myResponse = new Person();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(myResponse.GetType());
myResponse = serializer.ReadObject(ms) as Response;
ms.Close();
Where Response would be the type of object that represents the root of your json.
For more information visit the MSDN page of the DataContractJsonSerializer class.