简体   繁体   English

选择并从JObject删除嵌套的键值

[英]Select and remove a nested key value from JObject

I have a JObject for which I would like to check if a certain key value pair exists and if so get the value from the key and then remove the key. 我有一个JObject,我想检查该对象是否存在某个键值对,如果存在,则从键中获取值,然后删除键。

JObject-props

{
  "name": "Red Game",
  "id": "0060a00000alKw3AAE",
  "statecode": 0,
  "StudioId": {
    "pfstudioid": "B20996D68598FF7F"
  },
  "statuscode": 1,
  "lastapicall": "2018-10-11T00:00:00Z"
}

in my code I have: 在我的代码中,我有:

if (props.ContainsKey("StudioId.pfstudioid"))
{
    string value= props.GetValue("StudioId.pfstudioid")                      
    props.Remove("StudioId.pfstudioid");
}

But it doesn't find the that they key exists in the JObject and skips the if condition block. 但是,它没有发现它们的键存在于JObject中,而是跳过了if条件块。 How do I write this correctly? 我该如何正确写呢?

The problem isn't your .ContainsKey method, it's the props.Remove() . 问题不是您的.ContainsKey方法,而是props.Remove() You cannot use dot notation, to remove a subkey. 您不能使用点符号来删除子项。 You can delete this key this way: 您可以通过以下方式删除此密钥:

props.Value<JObject>("StudioId").Remove("pfstudioid");

See my .net fiddle here: https://dotnetfiddle.net/8mVEaa 在这里查看我的.net小提琴: https : //dotnetfiddle.net/8mVEaa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM