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

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

      <legend id='asYwl'><style id='asYwl'><dir id='asYwl'><q id='asYwl'></q></dir></style></legend>
        • <bdo id='asYwl'></bdo><ul id='asYwl'></ul>
        <tfoot id='asYwl'></tfoot>

        动态 JContainer (JSON.NET) &amp;在运行时迭代属性

        时间:2023-08-23
        <tfoot id='hVw5K'></tfoot>
            <i id='hVw5K'><tr id='hVw5K'><dt id='hVw5K'><q id='hVw5K'><span id='hVw5K'><b id='hVw5K'><form id='hVw5K'><ins id='hVw5K'></ins><ul id='hVw5K'></ul><sub id='hVw5K'></sub></form><legend id='hVw5K'></legend><bdo id='hVw5K'><pre id='hVw5K'><center id='hVw5K'></center></pre></bdo></b><th id='hVw5K'></th></span></q></dt></tr></i><div id='hVw5K'><tfoot id='hVw5K'></tfoot><dl id='hVw5K'><fieldset id='hVw5K'></fieldset></dl></div>

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

            2. <legend id='hVw5K'><style id='hVw5K'><dir id='hVw5K'><q id='hVw5K'></q></dir></style></legend>
                <tbody id='hVw5K'></tbody>

                  <bdo id='hVw5K'></bdo><ul id='hVw5K'></ul>
                • 本文介绍了动态 JContainer (JSON.NET) &amp;在运行时迭代属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在 MVC4/.NET4 WebApi 控制器操作中收到一个 JSON 字符串.该操作的参数是 dynamic,因为我对接收端收到的 JSON 对象一无所知.

                  I'm receiving a JSON string in a MVC4/.NET4 WebApi controller action. The action's parameter is dynamic because I don't know anything on the receiving end about the JSON object I'm receiving.

                   public dynamic Post(dynamic myobject)        
                  

                  自动解析 JSON,生成的 dynamic 对象是 Newtonsoft.Json.Linq.JContainer.正如预期的那样,我可以在运行时评估属性,因此如果 JSON 包含类似 myobject.myproperty 的内容,那么我现在可以获取接收到的动态对象并在 C# 代码中调用 myobject.myproperty.到目前为止一切顺利.

                  The JSON is automatically parsed and the resulting dynamic object is a Newtonsoft.Json.Linq.JContainer. I can, as expected, evaluate properties at runtime, so if the JSON contained something like myobject.myproperty then I can now take the dynamic object received and call myobject.myproperty within the C# code. So far so good.

                  现在我想遍历作为 JSON 的一部分提供的所有属性,包括嵌套属性.但是,如果我这样做 myobject.GetType().GetProperties() 它只返回 Newtonsoft.Json.Linq.JContainer 的属性,而不是我正在寻找的属性(是 JSON 的一部分).

                  Now I want to iterate over all properties that were supplied as part of the JSON, including nested properties. However, if I do myobject.GetType().GetProperties() it only returns properties of Newtonsoft.Json.Linq.JContainer instead of the properties I'm looking for (that were part of the JSON).

                  知道怎么做吗?

                  推荐答案

                  我认为这可以作为一个起点

                  I think this can be a starting point

                  dynamic dynObj = JsonConvert.DeserializeObject("{a:1,b:2}");
                  
                  //JContainer is the base class
                  var jObj = (JObject)dynObj;
                  
                  foreach (JToken token in jObj.Children())
                  {
                      if (token is JProperty)
                      {
                          var prop = token as JProperty;
                          Console.WriteLine("{0}={1}", prop.Name, prop.Value);
                      }
                  }
                  

                  编辑

                  这也可以帮助你

                  var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(jObj.ToString());
                  

                  这篇关于动态 JContainer (JSON.NET) &amp;在运行时迭代属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 DataContractJsonSerializer 反序列化变量类型 JS 下一篇:使用 json.net 在序列化期间合并两个对象?

                  相关文章

                  最新文章

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

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

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

                    <tfoot id='sB10S'></tfoot>