Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
703 views
in Technique[技术] by (71.8m points)

c# - Rename JProperty in json.net

I've the following property

{
  "bad": 
  {
    "Login": "someLogin",
    "Street": "someStreet",
    "House": "1",
    "Flat": "0",
    "LastIndication": 
    [
      "230",
      "236"
    ],
    "CurrentIndication": 
    [
      "263",
      "273"
    ],
    "Photo": 
    [
      null,
      null
    ]
  }
}

and how can I rename this from 'bad' to 'good' for example. Yes, I saw the extension method by Abi Bellamkonda

public static class NewtonsoftExtensions
{
    public static void Rename(this JToken token, string newName)
    {
        var parent = token.Parent;
        if (parent == null)
            throw new InvalidOperationException("The parent is missing.");
        var newToken = new JProperty(newName, token);
        parent.Replace(newToken);
    }
}

but it got this exeption

Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JProperty.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Somewhat counterintuitively, that extension method assumes that the token you are passing to it is the value of a JProperty, not the JProperty itself. Presumably, this is to make it easy to use with the square bracket syntax:

JObject jo = JObject.Parse(json);
jo["bad"].Rename("good");

If you have a reference to the property, you can still use that extension method if you call it on the property's Value like this:

JObject jo = JObject.Parse(json);
JProperty prop = jo.Property("bad");
prop.Value.Rename("good");

However, that makes the code seem confusing. It would be better to improve the the extension method so that it will work in both situations:

public static void Rename(this JToken token, string newName)
{
    if (token == null)
        throw new ArgumentNullException("token", "Cannot rename a null token");

    JProperty property;

    if (token.Type == JTokenType.Property)
    {
        if (token.Parent == null)
            throw new InvalidOperationException("Cannot rename a property with no parent");

        property = (JProperty)token;
    }
    else
    {
        if (token.Parent == null || token.Parent.Type != JTokenType.Property)
            throw new InvalidOperationException("This token's parent is not a JProperty; cannot rename");

        property = (JProperty)token.Parent;
    }

    // Note: to avoid triggering a clone of the existing property's value,
    // we need to save a reference to it and then null out property.Value
    // before adding the value to the new JProperty.  
    // Thanks to @dbc for the suggestion.

    var existingValue = property.Value;
    property.Value = null;
    var newProperty = new JProperty(newName, existingValue);
    property.Replace(newProperty);
}

Then you can do:

JObject jo = JObject.Parse(json);
jo.Property("bad").Rename("good");  // works with property reference
jo["good"].Rename("better");        // also works with square bracket syntax

Fiddle: https://dotnetfiddle.net/RSIdfx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...