简体   繁体   中英

JsonSerializer.Deserialize<T>(); in System.Text.Json not deserialize correctly

I'm trying to deserialize this json with System.Text.Json , but I can't, and I don't know why it's not working, here's the result:

PS: And I don't know if that influences, but the array where the elements are inside, has no name, just enter the link and see PS/2: This happens on any object in the list

在此处输入图像描述

Here is my code:

using System.Text.Json.Serialization;

namespace AceOfSpadesServersList
{
    internal sealed class ServerList
    {
        [JsonPropertyName("name")]
        public string Name { get; internal set; }

        [JsonPropertyName("identifier")]
        public string IP { get; internal set; }

        [JsonPropertyName("map")]
        public string Map { get; internal set; }

        [JsonPropertyName("game_mode")]
        public string GameMode { get; internal set; }

        [JsonPropertyName("country")]
        public string Country { get; internal set; }

        [JsonPropertyName("latency")]
        public ushort Latency { get; internal set; }

        [JsonPropertyName("players_current")]
        public byte CurrentPlayers { get; internal set; }

        [JsonPropertyName("players_max")]
        public byte MaxPlayers { get; internal set; }

        [JsonPropertyName("last_updated")]
        public uint LastUpdated { get; internal set; }

        [JsonPropertyName("game_version")]
        public string GameVersion { get; internal set; }
    }
}
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using AceOfSpadesServersList;

namespace AceOfSpadesServersList
{
    public class AceOfSpadesServersList
    {
        private const string BuildAndShootServersList = "http://services.buildandshoot.com/serverlist.json";

        private readonly HttpClient _httpClient;

        public AceOfSpadesServersList()
        {
            if (_httpClient is null)
                this._httpClient = new HttpClient();
        }

        public async Task GetAllServersAsync()
        {
            var json = string.Empty;

            var streamHttpResponse = await this._httpClient.GetStreamAsync(BuildAndShootServersList);
            using (var sr = new StreamReader(streamHttpResponse, Encoding.UTF8))
                json = await sr.ReadToEndAsync();

            var serverList = JsonSerializer.Deserialize<ServerList[]>(json);
        }
    }
}

I don't know what's wrong, but I tested the exact same code using Newtonsoft.Json , and I just changed the JsonPropertyName attribute to JsonProperty and JsonSerializer.Deserialize<ServerList[]>(json); to JsonConvert.DeserializeObject<ServerList[]>(json); and works normally, it just doesn't work in the standard C# library

System.Text.Json respects the visibility constraints you impose on the class. Remove the internal on your setters.

i have the same problem and fix it by add PropertyNamingPolicy

   var serverList = JsonSerializer.Deserialize<ServerList[]>(json, new 
    JsonSerializerOptions
    {
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM