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

      <tfoot id='Fnu4H'></tfoot>
        • <bdo id='Fnu4H'></bdo><ul id='Fnu4H'></ul>

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

      2. <legend id='Fnu4H'><style id='Fnu4H'><dir id='Fnu4H'><q id='Fnu4H'></q></dir></style></legend>
      3. 将部分 JSON 替换为其他(使用字符串标记)

        时间:2023-08-24

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

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

              • <legend id='gNbSn'><style id='gNbSn'><dir id='gNbSn'><q id='gNbSn'></q></dir></style></legend>

                  本文介绍了将部分 JSON 替换为其他(使用字符串标记)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个无法解决的问题,我在不同的地方寻找和寻找,但我仍然没有找到它.

                  I'm having an issue that I cant fix, I seek and seek in different places, but I still without find it.

                  查看此代码:

                  //I have 2 json to merge  
                  
                  var json = @"{'client': {'name': 'Should NO CHANGE', 'lastname': '', 'email': 'asd@asd.com', 'phone': '',  'birthday': '', 'married': false, 'spouse': {'name': '', 'lastname': ''} } }";
                  
                  var json2 = @" {'client': {'name': 'aaaa', 'lastname': 'bbbb', 'email': 'cccccc@ccccc.com',  'phone': '', 'birthday': '', 'married': false,  'spouse': {'name': 'dddddd', 'lastname': 'eeeee'} } } ";
                  
                  //for example to properties to replace
                  var path = "client.spouse";
                  var path2 = "client.email";
                  
                  dynamic o1 = JObject.Parse(json);
                  dynamic o2 = JObject.Parse(json2);
                  
                  
                  
                  //I want this, but using the string (LIKE REFLECTION) 
                  // NOT THE DYNAMIC object 
                  // Can be a simple property or a complex object
                  o1.client.spouse = o2.client.spouse;
                  o1.client.email = o2.client.email; 
                  

                  我需要使用字符串而不是o1.client.email"来替换内容.因为可以是任何东西.(json也可以是任何东西)

                  I need to use a string instead of "o1.client.email" to replace the content. Because can be anything. (also the json can be anything)

                  我可以用字符串、动态或任何可行的方式替换.

                  I can replace by strings, by dynamic, or whatever it work.

                  (XML 有效,但我丢失了日期、布尔值或数字时的数据类型)

                  (XML works, but I lost the data type when is a date, boolean or numeric)

                  NetFiddle 中的示例.

                  推荐答案

                  可以使用SelectToken 选择 JSON 对象层次结构中的任何项目.它支持 JSONPath 查询语法.它返回一个与所选项目的 value 对应的 JToken,如果没有找到,则返回 null.该 JToken 又具有 Replace(JToken替换) 方法.因此你可以这样做:

                  You can use SelectToken to select any item in the JSON object hierarchy. It supports JSONPath query syntax. It returns a JToken corresponding to the value of the selected item, or null if not found. That JToken in turn has a Replace(JToken replacement) method. Thus you can do:

                  var o1 = JObject.Parse(json);
                  var o2 = JObject.Parse(json2);
                  
                  var path = "client.spouse";
                  o1.SelectToken(path).Replace(o2.SelectToken(path));
                  
                  var path2 = "client.email";
                  o1.SelectToken(path2).Replace(o2.SelectToken(path2));
                  

                  这篇关于将部分 JSON 替换为其他(使用字符串标记)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 Json.NET 序列化为 NDJSON 下一篇:IEnumerable 中每个项目的自定义 json 序列化

                  相关文章

                  最新文章

                    <bdo id='4HwTj'></bdo><ul id='4HwTj'></ul>

                    <i id='4HwTj'><tr id='4HwTj'><dt id='4HwTj'><q id='4HwTj'><span id='4HwTj'><b id='4HwTj'><form id='4HwTj'><ins id='4HwTj'></ins><ul id='4HwTj'></ul><sub id='4HwTj'></sub></form><legend id='4HwTj'></legend><bdo id='4HwTj'><pre id='4HwTj'><center id='4HwTj'></center></pre></bdo></b><th id='4HwTj'></th></span></q></dt></tr></i><div id='4HwTj'><tfoot id='4HwTj'></tfoot><dl id='4HwTj'><fieldset id='4HwTj'></fieldset></dl></div>
                  1. <legend id='4HwTj'><style id='4HwTj'><dir id='4HwTj'><q id='4HwTj'></q></dir></style></legend>
                  2. <small id='4HwTj'></small><noframes id='4HwTj'>

                  3. <tfoot id='4HwTj'></tfoot>