在json.net中重命名JProperty [英] Rename JProperty in json.net

查看:85
本文介绍了在json.net中重命名JProperty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下属性

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

,以及如何将其从坏"重命名为好".是的,我看到了Abi Bellamkonda的扩展方法

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

但是它得到了成功

无法将Newtonsoft.Json.Linq.JProperty添加到 Newtonsoft.Json.Linq.JProperty.

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

推荐答案

以某种与直觉相反的方式,该扩展方法假定您要传递给它的tokenJProperty value ,不是JProperty本身.大概是为了使它易于使用方括号语法:

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

如果对属性有引用,则可以像下面这样在属性的Value上调用该扩展方法:

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

提琴: https://dotnetfiddle.net/RSIdfx

这篇关于在json.net中重命名JProperty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆