Свойство RootElement
RootElement имеет тип
JsonElement.
После
Dispose
обращение к свойству бросает
ObjectDisposedException.
Синтаксис
JsonElement root = doc.RootElement;
Пример
Давайте узнать вид корня объекта:
using System;
using System.Text.Json;
class Program
{
static void Main()
{
using JsonDocument doc = JsonDocument.Parse("{\"Name\":\"abcde\"}");
string res = doc.RootElement.ValueKind.ToString();
Console.WriteLine(res);
}
}
Результат выполнения кода:
"Object"
Пример
Давайте прочитать строковое поле:
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"
Пример
Давайте прочитать число в корне:
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
Смотрите также
-
Parse,
создание документа -
ValueKind,
вид узла -
GetProperty,
дочернее поле -
Dispose,
закрытие