I get from my data something like that:
ValueKind = Array : "[{"LookupId":370,"LookupValue":"Rahim, Zillur","Email":"[email protected]"}]"
VS says it is a JsonElement but I don't understand how to use JsonElement.TryGetProperty Method
if (JsonElement.TryGetProperty("Email", out value))
{
string email = value.GetString();
}
gives errors.
Anyone can help me with syntax to get from variable X that shows value
ValueKind = Array : "[{"LookupId":370,"LookupValue":"Rahim, Zillur","Email":"[email protected]"}]"
how to get just the email property?
Based on the provided info your json element seems to be an array so you need to process it as one. For example:
var js = """
[{"LookupId":370,"LookupValue":"Rahim, Zillur","Email":"[email protected]"}]
""";
using var jsonDocument = JsonDocument.Parse(js);
var jsonElement = jsonDocument.RootElement;
var emails = jsonElement.EnumerateArray()
.Select(element =>
{
if (element.TryGetProperty("Email", out var value))
{
return (Ok: true, value);
}
return (Ok: false, value);
})
.Where(t => t.Ok)
.Select(t => t.value);