使用TryGetProperty时忽略大小写 [英] Ignore case when using TryGetProperty

查看:178
本文介绍了使用TryGetProperty时忽略大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JsonElement类型的对象,其中包含API返回的数据.我想获取某个属性的值,但问题是TryGetProperty区分大小写.是否有一种方法或一种变通方法,可以忽略名称区分大小写而按名称获取属性?

I have an object of type JsonElement that contains data returned by an API. I want to get the value of a certain property but the problem is that TryGetProperty is case sensitive. Is there a way or a work around to get properties by name while ignoring case sensitivity?

谢谢.

推荐答案

EnumerateObject 将为您提供所有属性的列表.您可以遍历它们,找到第一个与名称匹配的名称,忽略大小写:

EnumerateObject will give you a list of all properties. You can loop through them and find the first one that matches the name ignoring case:

JsonElement value = null;
bool found = false;

var property = element.EnumerateObject()
                      .FirstOrDefault(p => string.Compare(p.Name, propName, 
                                                          StringComparison.OrdinalIgnoreCase) == 0);
if(property != null)
{
    value = property.Value;
    found = true;
}

这篇关于使用TryGetProperty时忽略大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆