Я пытаюсь использовать библиотеку System.text.json для повышения производительности. Когда я пытаюсь преобразовать его, System.text.json не способен десериализировать объект, но Newtonsoft.json может сделать это.using System;
using System.Text.Json;
using Newtonsoft.Json;
public class Person {
public string Name;
public string Role;
}
public class Program
{
public static void Main()
{
string json = """{"Name":"John","Role":"Software Engineer"}""";
using JsonDocument doc = JsonDocument.Parse(json);
JsonElement jsonElement = doc.RootElement;
var personText = System.Text.Json.JsonSerializer.Deserialize(jsonElement.GetRawText());
Console.WriteLine($"from System.Text.Json {personText.Role}");
var personNewton = JsonConvert.DeserializeObject(jsonElement.GetRawText());
Console.WriteLine($"from Newtonsoft.Json {personNewton.Role}");
}
}
< /code>
output: < /p>
from System.Text.Json
from Newtonsoft.Json Software Engineer
Подробнее здесь: https://stackoverflow.com/questions/796 ... t-correctl
System.text.json.jsoneserializer.deserialize не десериализует объект правильно [дублировать] ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
System.text.json.jsoneserializer.deserialize не десериализует объект правильно
Anonymous » » в форуме C# - 0 Ответы
- 2 Просмотры
-
Последнее сообщение Anonymous
-