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

    1. <legend id='mkbse'><style id='mkbse'><dir id='mkbse'><q id='mkbse'></q></dir></style></legend>

        <tfoot id='mkbse'></tfoot>

        • <bdo id='mkbse'></bdo><ul id='mkbse'></ul>

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

      1. 如何以编程方式从动态 JObject 获取属性

        时间:2023-05-22

            <tbody id='l7C6R'></tbody>
        1. <tfoot id='l7C6R'></tfoot>
        2. <legend id='l7C6R'><style id='l7C6R'><dir id='l7C6R'><q id='l7C6R'></q></dir></style></legend>
            <bdo id='l7C6R'></bdo><ul id='l7C6R'></ul>

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

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

                1. 本文介绍了如何以编程方式从动态 JObject 获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 NewtonSoft JObject 解析 JSON 字符串.如何以编程方式从动态对象中获取值?我想简化代码,不要为每个对象重复自己.

                  I'm parsing a JSON string using the NewtonSoft JObject. How can I get values from a dynamic object programmatically? I want to simplify the code to not repeat myself for every object.

                  public ExampleObject GetExampleObject(string jsonString)
                  {
                  ExampleObject returnObject = new ExampleObject();
                  dynamic dynamicResult = JObject.Parse(jsonString);
                  if (!ReferenceEquals(dynamicResult.album, null))
                     {
                         //code block to extract to another method if possible
                         returnObject.Id = dynamicResult.album.id; 
                         returnObject.Name = dynamicResult.album.name;
                         returnObject.Description = dynamicResult.albumsdescription;
                         //etc..
                     }
                  else if(!ReferenceEquals(dynamicResult.photo, null))
                     {
                         //duplicated here
                         returnObject.Id = dynamicResult.photo.id;
                         returnObject.Name = dynamicResult.photo.name;
                         returnObject.Description = dynamicResult.photo.description;
                         //etc..
                     }
                  else if..
                  //etc..
                  
                  return returnObject;
                  }
                  

                  有什么方法可以将if"语句中的代码块提取到单独的方法中,例如:

                  Is there any way I can extract the code blocks in the "if" statements to a separate method e.g:

                  private void ExampleObject GetExampleObject([string of desired type goes here? album/photo/etc])
                  {
                    ExampleObject returnObject = new ExampleObject();
                    returnObject.Id = dynamicResult.[something goes here?].id;
                    returnObject.Name = dynamicResult.[something goes here?].name;
                    //etc..
                    return returnObject;
                  }
                  

                  是否有可能,因为我们不能对动态对象使用反射.或者我是否正确使用了 JObject?

                  Is it even possible since we can't use reflection for dynamic objects. Or am I even using the JObject correctly?

                  谢谢.

                  推荐答案

                  假设您使用的是 Newtonsoft.Json.Linq.JObject,则不需要使用动态.JObject 类可以采用字符串索引器,就像字典一样:

                  Assuming you're using the Newtonsoft.Json.Linq.JObject, you don't need to use dynamic. The JObject class can take a string indexer, just like a dictionary:

                  JObject myResult = GetMyResult();
                  returnObject.Id = myResult["string here"]["id"];
                  

                  希望这会有所帮助!

                  这篇关于如何以编程方式从动态 JObject 获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Json.NET 不区分大小写的属性反序列化 下一篇:从类中获取 JSON 属性名称列表以在查询字符串中使用

                  相关文章

                  • <bdo id='UMPNB'></bdo><ul id='UMPNB'></ul>

                  <tfoot id='UMPNB'></tfoot>

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

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