简体   繁体   中英

How to get the parent key name of all sub json which has a parameter with key name "$oid"?

{
"id":{"$oid":5c9238241ea62ed5d516fcae},
"createdOn" : ISODate("2019-03-19T03:50:00.000Z"),
"version" : "1.0",
"ruleMasterID":{"$oid":5c90df381ea62ed5d5138266},
}

i have a json like this. i want the key name where "$oid" exists as key in sub json. so, in this json id and ruleMasterID are the expecting output. can anyone tell me that how to write code to get the same in C#?

If you don't want a recursive search, then you could simply do a LINQ statement:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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