<small id='ljy9F'></small><noframes id='ljy9F'>

    <i id='ljy9F'><tr id='ljy9F'><dt id='ljy9F'><q id='ljy9F'><span id='ljy9F'><b id='ljy9F'><form id='ljy9F'><ins id='ljy9F'></ins><ul id='ljy9F'></ul><sub id='ljy9F'></sub></form><legend id='ljy9F'></legend><bdo id='ljy9F'><pre id='ljy9F'><center id='ljy9F'></center></pre></bdo></b><th id='ljy9F'></th></span></q></dt></tr></i><div id='ljy9F'><tfoot id='ljy9F'></tfoot><dl id='ljy9F'><fieldset id='ljy9F'></fieldset></dl></div>
    • <bdo id='ljy9F'></bdo><ul id='ljy9F'></ul>
    <legend id='ljy9F'><style id='ljy9F'><dir id='ljy9F'><q id='ljy9F'></q></dir></style></legend>
      <tfoot id='ljy9F'></tfoot>

      在 json.net 中重命名 JProperty

      时间:2023-08-23

      <small id='4VN40'></small><noframes id='4VN40'>

          <tbody id='4VN40'></tbody>

          <tfoot id='4VN40'></tfoot>

            • <bdo id='4VN40'></bdo><ul id='4VN40'></ul>
                <legend id='4VN40'><style id='4VN40'><dir id='4VN40'><q id='4VN40'></q></dir></style></legend>

                <i id='4VN40'><tr id='4VN40'><dt id='4VN40'><q id='4VN40'><span id='4VN40'><b id='4VN40'><form id='4VN40'><ins id='4VN40'></ins><ul id='4VN40'></ul><sub id='4VN40'></sub></form><legend id='4VN40'></legend><bdo id='4VN40'><pre id='4VN40'><center id='4VN40'></center></pre></bdo></b><th id='4VN40'></th></span></q></dt></tr></i><div id='4VN40'><tfoot id='4VN40'></tfoot><dl id='4VN40'><fieldset id='4VN40'></fieldset></dl></div>

                本文介绍了在 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.

                推荐答案

                有点违反直觉,该扩展方法假定您传递给它的 tokenvalueJProperty,而不是 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);
                }
                

                那么你可以这样做:

                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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:JSON.Net:反序列化多态类型而不指定程序集 下一篇:JSON.net - 字段是字符串或列表&lt;字符串&g

                相关文章

                最新文章

                1. <small id='be365'></small><noframes id='be365'>

                    <bdo id='be365'></bdo><ul id='be365'></ul>

                  <legend id='be365'><style id='be365'><dir id='be365'><q id='be365'></q></dir></style></legend>

                  <tfoot id='be365'></tfoot>
                  <i id='be365'><tr id='be365'><dt id='be365'><q id='be365'><span id='be365'><b id='be365'><form id='be365'><ins id='be365'></ins><ul id='be365'></ul><sub id='be365'></sub></form><legend id='be365'></legend><bdo id='be365'><pre id='be365'><center id='be365'></center></pre></bdo></b><th id='be365'></th></span></q></dt></tr></i><div id='be365'><tfoot id='be365'></tfoot><dl id='be365'><fieldset id='be365'></fieldset></dl></div>