a developer's notes – a semi-technical web development BLOG

November 15, 2013

Remove all properties from Json using JSON.NET

Filed under: JSON — Duy Nguyen @ 9:47 am
Tags: , , , , , , , ,

Method to remove a property from a JToken. Taken from http://stackoverflow.com/questions/11676159/json-net-how-to-remove-nodes

private void removeFields(JToken token, string[] fields)
{
    JContainer container = token as JContainer;
    if (container == null) return;

    List<JToken> removeList = new List<JToken>();
    foreach (JToken el in container.Children())
    {
        JProperty p = el as JProperty;
        if (p != null && fields.Contains(p.Name))
        {
            removeList.Add(el);
        }
        removeFields(el, fields);
    }

    foreach (JToken el in removeList)
    {
        el.Remove();
    }
}
Advertisements

1 Comment »

  1. Reblogged this on Sutoprise Avenue, A SutoCom Source.

    Comment by SutoCom — November 20, 2013 @ 7:10 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: