I trying to fetch data from this api point: click here
All objects come null, and I don’t know why.
The response is 200 ok, but every object is null.
using System;
using System.Collections.Generic;
using System.Text;
namespace pizhevsoft.Models.API
{
internal class Voltage_AMS_AHS
{
public string StationName { get; set; }
public int StationNumber { get; set; }
public double? dayHour { get; set; }
public double? dayHour1 { get; set; }
public double? dayHour2 { get; set; }
public double? dayHour3 { get; set; }
public double? dayHour4 { get; set; }
public double? dayHour5 { get; set; }
public double? dayHour6 { get; set; }
public double? dayHour7 { get; set; }
public double? dayHour8 { get; set; }
public double? avarage { get; set; }
}
}
Here I set getters and setters for the properties.
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using pizhevsoft.Models.API;
namespace pizhevsoft.Services
{
internal class RestService_Voltage
{
HttpClient _client;
public RestService_Voltage()
{
_client = new HttpClient();
}
public async Task<List<Voltage_AMS_AHS>> Get_Voltage_AMS_AHS(string query)
{
List<Voltage_AMS_AHS> Get_Voltage_AMS_AHS = new List<Voltage_AMS_AHS>();
try
{
var response = await _client.GetAsync(query);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
Get_Voltage_AMS_AHS = JsonConvert.DeserializeObject<List<Voltage_AMS_AHS>>(content);
}
}
catch (Exception ex)
{
Debug.WriteLine("ttERROR {0}", ex.Message);
}
return Get_Voltage_AMS_AHS;
}
}
}
In this code I use httpClient to get data, and the content have 157 objects but all of these is null.
What do I need to do to see the data from API point ?
UPDATE:
When I trying to use System.Text.Json.Serialization library the objects is still null:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json.Serialization;
namespace pizhevsoft.Models.API
{
internal class Voltage_AMS_AHS
{
[JsonPropertyName("Станция име")]
public string StationName { get; set; }
[JsonPropertyName("Номер")]
public int StationNumber { get; set; }
[JsonPropertyName("ден/час")]
public double? dayHour { get; set; }
[JsonPropertyName("ден/час1")]
public double? dayHour1 { get; set; }
[JsonPropertyName("ден/час2")]
public double? dayHour2 { get; set; }
[JsonPropertyName("ден/час3")]
public double? dayHour3 { get; set; }
[JsonPropertyName("ден/час4")]
public double? dayHour4 { get; set; }
[JsonPropertyName("ден/час5")]
public double? dayHour5 { get; set; }
[JsonPropertyName("ден/час6")]
public double? dayHour6 { get; set; }
[JsonPropertyName("ден/час7")]
public double? dayHour7 { get; set; }
[JsonPropertyName("ден/час8")]
public double? dayHour8 { get; set; }
[JsonPropertyName("средно")]
public double? avarage { get; set; }
}
}
This is raw view when I use System.Text.Json.Serialization
When I trying with Newtonsoft.Json with [JsonProperty("Станция име")] the objects not coming ..
2
Answers
The response of that API contains properties with cyrillic letters, when I call it:
So, there are at least two problems here:
The first item in the response array does not containdouble
values, but instead provides a date, which can either be parsed to aDateTime
or astring
string
instead ofdouble?
orint
Generally, property names with cyrillic letters cannot be serialized to properties with English names using the latin alphabet. Does that API offer to provide a query parameter to set the language?
If not, you will need to specify the JSON property name like this using System.Text.Json.Serialization and use the correct data type:
This can also be done similarly using Newtonsoft.Json, there the equivalent is called
[JsonProperty]
:The values are all provided as
string
, probably, because the format of the data changes (if you compare the first and second item in the JSON array). You will need to convert the data types yourself by parsing them to the correct type a second step.you have two parts of your data – the first item of array is a header, the rest is data. So you can get data using this code
you have to update your question to tell us what to do with a header arrHeader. You can create another class with DateTime fields and deserialize it or leave as it is