【问题标题】:How to get the parent key name of all sub json which has a parameter with key name "$oid"?如何获取具有键名“$oid”的参数的所有子json的父键名?
【发布时间】:2019-03-26 23:03:06
【问题描述】:
{
"id":{"$oid":5c9238241ea62ed5d516fcae},
"createdOn" : ISODate("2019-03-19T03:50:00.000Z"),
"version" : "1.0",
"ruleMasterID":{"$oid":5c90df381ea62ed5d5138266},
}

我有一个这样的 json。我想要“$oid”作为子 json 中的键存在的键名。 所以,在这个 json id 和 ruleMasterID 是预期的输出。 谁能告诉我如何编写代码以在 C# 中实现相同的功能?

【问题讨论】:

    标签: c# json mongodb jobjectformatter


    【解决方案1】:

    如果您不想要递归搜索,那么您可以简单地执行 LINQ 语句:

    JObject jsonObject = JObject.Parse(json);
    var keys = (from x in jsonObject.Children() where x.Children().Any(y => (y as JObject)?.ContainsKey("$oid")==true) select x.Path).ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 2019-05-01
      相关资源
      最近更新 更多