JsonElement
JsonElement получают из
RootElement
или методов вроде
GetProperty.
Узел нельзя изменить присваиванием.
Вид узла -
ValueKind.
Синтаксис
JsonElement element = doc.RootElement;
Пример
Давайте прочитать строку из узла:
using System;
using System.Text.Json;
class Program
{
static void Main()
{
using JsonDocument doc = JsonDocument.Parse("\"abcde\"");
string res = doc.RootElement.GetString();
Console.WriteLine(res);
}
}
Результат выполнения кода:
"abcde"
Пример
Давайте прочитать число из узла:
using System;
using System.Text.Json;
class Program
{
static void Main()
{
using JsonDocument doc = JsonDocument.Parse("12345");
int res = doc.RootElement.GetInt32();
Console.WriteLine(res);
}
}
Результат выполнения кода:
12345
Пример
Давайте прочитать поле объекта:
using System;
using System.Text.Json;
class Program
{
static void Main()
{
using JsonDocument doc = JsonDocument.Parse("{\"Name\":\"abcde\"}");
string res = doc.RootElement.GetProperty("Name").GetString();
Console.WriteLine(res);
}
}
Результат выполнения кода:
"abcde"
Смотрите также
-
JsonDocument,
документ -
ValueKind,
вид узла -
TryGetProperty,
поле без исключения -
JsonNode,
изменяемый узел