无法验证使用 jsonElement.TryGetProperty 加载到 JsonElement 中的 json 字符串中的元素和值

Unable to validate elements and value from json string loaded into JsonElement with jsonElement.TryGetProperty

我正在使用 .net core 3.1 和 System.Text.Json。 我在 json 的子元素中有一个字符串,其中包含我想在某些 .net 元素中加载的元素声明,然后验证其中的键

{
  "scope": "openid MyScop",
  "claims": "{\"premiuminfo\":{\"country\":{\"value\":\"country1\"},\"town\":{\"value\":\"town1\"},\"given_name\":{\"value\":\"given_name1\"},\"postal_code\":{\"value\":\"postal_code1\"},\"family_name\":{\"value\":\"family_name1\"},\"houseno_or_housename\":{\"value\":\"test house number\"}}}",  
}

我能够在 JsonElement 中加载声明对象

 JsonElement o = JsonSerializer.Deserialize<JsonElement>(s);

但无法找到任何方法来检查 premiuminfo 、县 e.t.c 等密钥。

有人可以帮助我使用它吗

您可能正在寻找 TryGetProperty

TryGetProperty(ReadOnlySpan, JsonElement)

Looks for a property named propertyName in the current object, returning a value that indicates whether or not such a property exists. When the property exists, the method assigns its value to the value argument.

我建议使用 Newtonsoft.Json 库,它可以为您提供更多功能。您可以解析您的主要 json,然后解析您拥有的用于声明的字符串对象,以访问声明中的属性。

例子

var obj = JObject.Parse(json);
var claims = JObject.Parse(obj["claims"].ToString());

Console.WriteLine(claims["premiuminfo"].ToString()); // Prints entire claim
Console.WriteLine(claims["premiuminfo"]["country"].ToString()); // prints the country.

错误地使用了 TryGetProperty 以下是我能够验证的方式

string s = "{\"premiuminfo\":{\"country\":{\"value\":\"country1\"},\"town\":{\"value\":\"town1\"},\"given_name\":{\"value\":\"given_name1\"},\"postal_code\":{\"value\":\"postal_code1\"},\"family_name\":{\"value\":\"family_name1\"},\"houseno_or_housename\":{\"value\":\"houseno_or_housename1\"}}}";
//s = jwtData.ContainsKey("claims");
try
{
    JsonElement o = JsonSerializer.Deserialize<JsonElement>(s);

    if (o.TryGetProperty("premiuminfo", out var premiuminfo))
    {
        if (!premiuminfo.TryGetProperty("name", out var _) && (!premiuminfo.TryGetProperty("given_name", out var _) || !premiuminfo.TryGetProperty("family_name", out var _)))
        {
            processingResult = new ProcessingResultObject { ErrorResult = new ErrorResult { error = ErrorTypes.invalid_request.ToString(), error_description = "name or given_name, family_name is missing in claims" }, StatusCode = 400 };
        }
        else if (premiuminfo.TryGetProperty("name", out var _) && (premiuminfo.TryGetProperty("given_name", out var _) || !premiuminfo.TryGetProperty("family_name", out var _)))
        {
            processingResult = new ProcessingResultObject { ErrorResult = new ErrorResult { error = ErrorTypes.invalid_request.ToString(), error_description = "name and (given_name, family_name) both exists in claims" }, StatusCode = 400 };
        }
        else if (!premiuminfo.TryGetProperty("address", out var _) && (!premiuminfo.TryGetProperty("houseno_or_housename", out var _) || !premiuminfo.TryGetProperty("postal_code", out var _)))
        {
            processingResult = new ProcessingResultObject { ErrorResult = new ErrorResult { error = ErrorTypes.invalid_request.ToString(), error_description = "address is missing in claims" }, StatusCode = 400 };
        }
        else if (premiuminfo.TryGetProperty("address", out var _) && (premiuminfo.TryGetProperty("houseno_or_housename", out var _) || premiuminfo.TryGetProperty("postal_code", out var _)))
        {
            processingResult = new ProcessingResultObject { ErrorResult = new ErrorResult { error = ErrorTypes.invalid_request.ToString(), error_description = "address and (houseno_or_housename, postal_code) both exists in claims" }, StatusCode = 400 };
        }
        else
        {
            processingResult.Result = true;
        }
    }
    else
    {
        processingResult = new ProcessingResultObject { ErrorResult = new ErrorResult { error = ErrorTypes.invalid_request.ToString(), error_description = "premiuminfo are not found in claims" }, StatusCode = 400 };
    }
}
catch (Exception ex)
{
    processingResult = new ProcessingResultObject { ErrorResult = new ErrorResult { error = ErrorTypes.invalid_request.ToString(), error_description = "premiuminfo are not found in claims. ex" }, StatusCode = 400 };
}