简体   繁体   中英

Is there a way to remove nodes from JSON.NET JObject of a certain type?

I've got a JSON object that's returned from an API and some of the nodes are arrays. Is there any way for me to pull those out of the object completely based on the "type" ?

for example:

{ "result" : {
"field1": "value1",
"field2" : [ "val2", "val3" ],
"field3" : "val4",
"field4" : "val5" }
}

I'd like to be able to remove "field2" because it's an array.

i'm not sure how to iterate through the object in a way that will give me the type of the object.

I'm using C# and JSON.NET 6.0.5

thanks!

解析数据后执行以下操作:

jsonObject.Property("field2").Remove();

I think I've found the answer. Since you can use foreach with a JSON Object, all you have to do is check the type and choose to 'continue' or not

example:

foreach (var item in jobj) 
{
  if ( jobj[item.Key] is JArray ) 
  {
     continue;
  }
    // do what you would do with other types.
}

Thanks!

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