-1

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("\t\tERROR {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

Ben Johnson
  • 753
  • 1
  • 9
  • 18
  • Did you maybe forget to pass a query parameter to define the language of the response? When I call that API, the JSON properties are all in cyrillic letters. This would explain why your DTO's properties are null. Did you inspect the raw response? What does it look like for you? – Julian Jul 21 '23 at 08:08

2 Answers2

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.

Julian
  • 5,290
  • 1
  • 17
  • 40
  • I trying your method but with Newtonsoft.Json the object not coming, with System.Text.Json.Serialization the objects still coming null ;( Whyyy – Ben Johnson Jul 21 '23 at 08:44
  • Why did you accept the answer already, then? Did you inspect the raw string in the debugger? Are there any exceptions or errors that come from the serialization? – Julian Jul 21 '23 at 08:55
  • How to deserializable this line with system.text.json ? var content = await response.Content.ReadAsStringAsync(); Get_Voltage_AMS_AHS = JsonConvert.DeserializeObject>(content); – Ben Johnson Jul 21 '23 at 08:57
  • I updated my answer, because I noticed that the values are exclusively provided as strings, so you need to deserialize them to `string`. – Julian Jul 21 '23 at 09:03
  • Again come null everywhere.. – Ben Johnson Jul 21 '23 at 09:17
  • 1
    Again, why did you accept the answer if it did not solve your problem? – Jason Jul 21 '23 at 12:28
  • Exactly, if it didn't solve your problem, don't accept the answer and provide new or additional details instead. – Julian Jul 21 '23 at 12:35
1

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

Serge
  • 40,935
  • 4
  • 18
  • 45
  • I just want to get all objects and paste it in the grid system... that's it – Ben Johnson Jul 21 '23 at 10:35
  • What the grid system is? You have some data in DateTime format, some in double. How are you going to convert it to one class? You can only use Parse in this case or ask your boss what he wants – Serge Jul 21 '23 at 10:37