You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何检测JSON中是否包含特定值并进行比较(C#场景)

实现JSON值的检测与比较(基于你的C#架构)

嘿,看你已经搭好了从嵌入资源读取JSON、反序列化并传递给前端存储的基础框架啦,接下来咱们一步步把JSON值的检测和比较逻辑加上,完全贴合你现有的代码结构~

1. 先补全你的数据结构(方便后续操作)

你代码里的TheInfo和TheNa...类只写了一部分,先假设完整结构是这样(你可以根据实际情况调整):

public class TheInfo
{
    // 示例单个属性:品牌名称
    public string BrandName { get; set; }
    // 你提到的列表,假设是TheName类的集合
    public List<TheName> NameList { get; set; }
}

public class TheName
{
    public string Id { get; set; }
    public string DisplayName { get; set; }
    public int Priority { get; set; }
}

2. 后端读取JSON时的检测与比较

如果你想在后端反序列化完成后就做检测,直接操作ParsedBrandJSON对象就行:

检测单个属性是否匹配目标值

比如检查BrandName是否等于你要找的品牌名:

var targetBrand = "MyTargetBrand";
// 先判断属性是否存在(反序列化后不存在的属性会是默认值,比如string为null)
if (ParsedBrandJSON.BrandName != null)
{
    // 比较值,这里用忽略大小写的比较更灵活
    bool isMatch = ParsedBrandJSON.BrandName.Equals(targetBrand, StringComparison.OrdinalIgnoreCase);
    if (isMatch)
    {
        // 匹配成功后的逻辑,比如打日志、触发后续流程
        Console.WriteLine("品牌名称匹配成功!");
    }
}

检测列表中是否包含特定元素

比如检查NameList里有没有DisplayName是"SpecialName"的项:

var targetDisplayName = "SpecialName";
// 用?.避免列表为null的情况,?? false处理列表不存在的场景
bool hasTargetName = ParsedBrandJSON.NameList?.Any(item => item.DisplayName == targetDisplayName) ?? false;
if (hasTargetName)
{
    // 找到匹配项后可以进一步获取它的信息
    var matchedItem = ParsedBrandJSON.NameList.First(item => item.DisplayName == targetDisplayName);
    Console.WriteLine($"找到匹配项:ID={matchedItem.Id}");
}

3. 前端(TheInfoJSON存储后)的检测与比较

如果要在客户端代码里基于TheInfoJSON.Data做检测,推荐给静态类封装检测方法,这样调用更方便:

给TheInfoJSON添加检测方法

static public class TheInfoJSON
{
    static public TheInfo Data { get; set; }
    static public async Task LoadJson(TheInfo JSON) { Data = JSON; }

    // 封装:检测品牌名称是否匹配
    static public bool IsBrandMatch(string targetBrand)
    {
        if (Data?.BrandName == null) return false;
        return Data.BrandName.Equals(targetBrand, StringComparison.OrdinalIgnoreCase);
    }

    // 封装:根据DisplayName查找列表项
    static public TheName FindNameByDisplayName(string displayName)
    {
        return Data?.NameList?.FirstOrDefault(item => item.DisplayName == displayName);
    }

    // 封装:比较两个Name项的优先级
    static public int CompareNamePriority(TheName name1, TheName name2)
    {
        if (name1 == null || name2 == null) return 0;
        return name1.Priority.CompareTo(name2.Priority);
    }
}

调用示例

在其他业务代码里直接用这些方法就行:

// 检测品牌是否匹配
bool brandMatch = TheInfoJSON.IsBrandMatch("MyTargetBrand");
if (brandMatch)
{
    // 执行你的业务逻辑
}

// 查找指定名称的项
var matchedName = TheInfoJSON.FindNameByDisplayName("SpecialName");
if (matchedName != null)
{
    // 和另一个项比较优先级
    var anotherName = TheInfoJSON.FindNameByDisplayName("AnotherName");
    int priorityCompareResult = TheInfoJSON.CompareNamePriority(matchedName, anotherName);
    if (priorityCompareResult > 0)
    {
        Console.WriteLine("匹配项的优先级更高!");
    }
}

4. 几个关键注意点

  • 空值处理:一定要用?.和??处理可能为null的对象/属性,避免空引用异常。
  • 字符串比较:如果不需要区分大小写,用StringComparison.OrdinalIgnoreCase比直接==更可靠,能避免系统文化差异带来的问题。
  • 列表性能:用Any()判断元素是否存在比FirstOrDefault() != null更高效,因为Any()找到匹配项就会停止遍历。

内容的提问来源于stack exchange,提问作者Carlos Rodrigez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:16:52