skip to Main Content

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:

null objects

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

enter image description here

When I trying with Newtonsoft.Json with [JsonProperty("Станция име")] the objects not coming ..

enter image description here

2

Answers


  1. The response of that API contains properties with cyrillic letters, when I call it:

    [
      {
        "Станция име": "Станция име",
        "Номер": "Номер",
        "ден/час": "07/20/08",
        "ден/час1": "07/20/11",
        "ден/час2": "07/20/14",
        "ден/час3": "07/20/17",
        "ден/час4": "07/20/20",
        "ден/час5": "07/20/23",
        "ден/час6": "07/21/02",
        "ден/час7": "07/21/05",
        "средно": "средно",
        "ден/час8": "07/21/08"
      },
      {
        "Станция име": "Говежда",
        "Номер": "2417",
        "ден/час": "13.8",
        "ден/час1": "13.5",
        "ден/час2": "13.6",
        "ден/час3": "13.5",
        "ден/час4": "13.2",
        "ден/час5": "13",
        "ден/час6": "12.9",
        "ден/час7": "12.8",
        "средно": "13.3",
        "ден/час8": "13.7"
      },
      ...
    ]
    

    So, there are at least two problems here:

    1. The response contains cyrillic letters and you don’t provide the appropriate property names for the serializer to match them against the DTO’s property names
    2. The first item in the response array does not contain double values, but instead provides a date, which can either be parsed to a DateTime or a string
    3. The values are provided as strings, so you need to parse them to string instead of double? or int

    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:

    internal class Voltage_AMS_AHS
    {
        [JsonPropertyName("Станция име")]
        public string StationName { get; set; }
    
        [JsonPropertyName("Номер")]
        public string StationNumber { get; set; }
    
        [JsonPropertyName("ден/час")]
        public string dayHour { get; set; }
    
        [JsonPropertyName("ден/час1")]
        public string dayHour1 { get; set; }
    
        [JsonPropertyName("ден/час2")]
        public string dayHour2 { get; set; }
    
        [JsonPropertyName("ден/час3")]
        public string dayHour3 { get; set; }
    
        [JsonPropertyName("ден/час4")]
        public string dayHour4 { get; set; }
    
        [JsonPropertyName("ден/час5")]
        public string dayHour5 { get; set; }
    
        [JsonPropertyName("ден/час6")]
        public string dayHour6 { get; set; }
    
        [JsonPropertyName("ден/час7")]
        public string dayHour7 { get; set; }
    
        [JsonPropertyName("ден/час8")]
        public string dayHour8 { get; set; }
    
        [JsonPropertyName("средно")]
        public string avarage { get; set; }
    }
    

    This can also be done similarly using Newtonsoft.Json, there the equivalent is called [JsonProperty]:

    internal class Voltage_AMS_AHS
    {
        [JsonProperty("Станция име")]
        public string StationName { get; set; }
    
        [JsonProperty("Номер")]
        public string StationNumber { get; set; }
    
        [JsonProperty("ден/час")]
        public string dayHour { get; set; }
    
        // and so on...
    }
    

    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.

    Login or Signup to reply.
  2. 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

    
        var jArray = System.Text.Json.Nodes.JsonArray.Parse(json).AsArray();
        var arrHeader = jArray[0];
        jArray.RemoveAt(0);
    
        List<Voltage_AMS_AHS> result = System.Text.Json.JsonSerializer.Deserialize<List<Voltage_AMS_AHS>>(jArray, new JsonSerializerOptions
        {
            NumberHandling = JsonNumberHandling.AllowReadingFromString
        }); ;
    

    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search