【问题标题】:Checking whether a unique data is present in a json file检查 json 文件中是否存在唯一数据
【发布时间】:2018-05-22 10:43:16
【问题描述】:

我需要检查一个单词是否存在于 JSON 文件中。因此,如果我正在搜索“root”,那么即使“byroots”这个词包含 root,它也应该给我错误。 这是我的代码

using (StreamReader r = new StreamReader("filename.json"))
{
    string json1 = r.ReadToEnd();
    if (json1.Contains("root"))
    {
        filename = path + @"" + branch + "-" + testsuite.Title + ".json";
    }
}

我也试过这种情况:-

if (json1.IndexOf(testsuite.Title, StringComparison.OrdinalIgnoreCase) >= 0)

但我得到了相同的结果。

这是json数据

{
"LV": {
    "build_number": "20180517.1",
    "blah_blah": "blah",
    "name": "byroots",
    }
}

【问题讨论】:

  • json 数据长什么样?仅当 root 仅作为一个完整的单词存在时才返回 true 吗?如果json中存在root check之类的东西,它应该返回true还是false?
  • 我现在已经添加了 json 数据。并且只有当 root 作为“name”关键字中的单个单词存在时,它才应该返回 true。 @Chetan Ranpariya
  • 我认为你应该在 json 中搜索 "root"。或者可能是"name": "roots"

标签: c# json c#-4.0 json.net


【解决方案1】:

你应该使用正则表达式

var pattern = @"*root*";
Regex rgx = new Regex(pattern);
using (StreamReader r = new StreamReader("filename.json"))
{
 string json1 = r.ReadToEnd();
 if (rgx.IsMatch(json1))
 {
    filename = path + @"" + branch + "-" + testsuite.Title + ".json";
 }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多