简体   繁体   English

JsonSerializer.反序列化<t> (); 在 System.Text.Json 中没有正确反序列化</t>

[英]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:我正在尝试使用System.Text.Json反序列化json,但我不能,而且我不知道为什么它不起作用,结果如下:

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 PS:我不知道这是否会影响,但是元素所在的数组没有名称,只需输入链接并查看PS/2:列表中的任何 object 都会发生这种情况

在此处输入图像描述

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);我不知道出了什么问题,但我使用Newtonsoft.Json测试了完全相同的代码,我只是将JsonPropertyName属性更改为JsonPropertyJsonSerializer.Deserialize<ServerList[]>(json); to JsonConvert.DeserializeObject<ServerList[]>(json);JsonConvert.DeserializeObject<ServerList[]>(json); and works normally, it just doesn't work in the standard C# library并正常工作,只是在标准 C# 库中不起作用

System.Text.Json respects the visibility constraints you impose on the class. System.Text.Json尊重您对 class 施加的可见性约束。 Remove the internal on your setters.删除您的二传手的internal

i have the same problem and fix it by add PropertyNamingPolicy我有同样的问题并通过添加 PropertyNamingPolicy 来解决它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Text.Json JsonSerializer.反序列化<tvalue> (...) 无法反序列化 object[] 类型</tvalue> - System.Text.Json JsonSerializer.Deserialize<TValue>(...) cannot deserialize object[] type 第一次使用 NewtonSoft (JsonConvert.DeserializeObject&lt;&gt;() 反序列化 JSON 与 System.Text.Json (JsonSerializer.Deserialize&lt;&gt;() - First time using NewtonSoft (JsonConvert.DeserializeObject<>() for deserializing JSON versus System.Text.Json (JsonSerializer.Deserialize<>() System.Text:JsonSerializer.Deserialize 与泛型 - System.Text: JsonSerializer.Deserialize with generics JsonSerializer.Deserialize 失败 - JsonSerializer.Deserialize fails JsonSerializer.Deserialize 无法推断用法 - JsonSerializer.Deserialize can't inferred the usage 将 JSON 反序列化为 Payload <List<Post> &gt; 使用 JsonSerializer.Deserialize - Deserialize JSon to Payload<List<Post>> using JsonSerializer.Deserialize System.Text.JSON 不反序列化 Newtonsoft 所做的 - System.Text.JSON doesn't deserialize what Newtonsoft does JsonSerializer.Deserialize 异常:JSON 值无法转换为 System.String - JsonSerializer.Deserialize exception: The JSON value could not be converted to System.String 使用 System.Text.Json 反序列化匿名类型 - Deserialize anonymous type with System.Text.Json 将 false 反序列化为 null (System.Text.Json) - Deserialize false as null (System.Text.Json)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM