<bdo id='5UT2A'></bdo><ul id='5UT2A'></ul>
    <legend id='5UT2A'><style id='5UT2A'><dir id='5UT2A'><q id='5UT2A'></q></dir></style></legend>

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

      <tfoot id='5UT2A'></tfoot>

      <small id='5UT2A'></small><noframes id='5UT2A'>

        使用 Json.NET 将任何类型的对象转换为 JObject

        时间:2023-08-22

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

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

                • <i id='aW2vC'><tr id='aW2vC'><dt id='aW2vC'><q id='aW2vC'><span id='aW2vC'><b id='aW2vC'><form id='aW2vC'><ins id='aW2vC'></ins><ul id='aW2vC'></ul><sub id='aW2vC'></sub></form><legend id='aW2vC'></legend><bdo id='aW2vC'><pre id='aW2vC'><center id='aW2vC'></center></pre></bdo></b><th id='aW2vC'></th></span></q></dt></tr></i><div id='aW2vC'><tfoot id='aW2vC'></tfoot><dl id='aW2vC'><fieldset id='aW2vC'></fieldset></dl></div>
                  <legend id='aW2vC'><style id='aW2vC'><dir id='aW2vC'><q id='aW2vC'></q></dir></style></legend>
                • <tfoot id='aW2vC'></tfoot>
                    <tbody id='aW2vC'></tbody>
                  本文介绍了使用 Json.NET 将任何类型的对象转换为 JObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在使用 WebAPI 将其返回给客户端之前,我经常需要使用附加信息扩展我的域模型.为了避免创建 ViewModel,我想我可以返回带有附加属性的 JObject.但是,我无法找到直接方法将任何类型的对象转换为 JObject,只需调用 Newtonsoft JSON 库即可.我想出了这样的事情:

                  I often need to extend my Domain model with additional info before returning it to the client with WebAPI. To avoid creation of ViewModel I thought I could return JObject with additional properties. I could not however find direct way to convert object of any type to JObject with single call to Newtonsoft JSON library. I came up with something like this:

                  1. 第一个 SerializeObject
                  2. 然后解析
                  3. 并扩展 JObject

                  例如:

                  var cycles = cycleSource.AllCycles();
                  
                  var settings = new JsonSerializerSettings
                  {
                      ContractResolver = new CamelCasePropertyNamesContractResolver()
                  };
                  
                  var vm = new JArray();
                  
                  foreach (var cycle in cycles)
                  {
                      var cycleJson = JObject.Parse(JsonConvert.SerializeObject(cycle, settings));
                      // extend cycleJson ......
                      vm.Add(cycleJson);
                  }
                  
                  return vm;
                  

                  我这个方法正确吗?

                  推荐答案

                  JObject 实现了 IDictionary,所以你可以这样使用它.例如,

                  JObject implements IDictionary, so you can use it that way. For ex,

                  var cycleJson  = JObject.Parse(@"{""name"":""john""}");
                  
                  //add surname
                  cycleJson["surname"] = "doe";
                  
                  //add a complex object
                  cycleJson["complexObj"] = JObject.FromObject(new { id = 1, name = "test" });
                  

                  所以最终的 json 将是

                  So the final json will be

                  {
                    "name": "john",
                    "surname": "doe",
                    "complexObj": {
                      "id": 1,
                      "name": "test"
                    }
                  }
                  

                  你也可以使用动态关键字

                  dynamic cycleJson  = JObject.Parse(@"{""name"":""john""}");
                  cycleJson.surname = "doe";
                  cycleJson.complexObj = JObject.FromObject(new { id = 1, name = "test" });
                  

                  这篇关于使用 Json.NET 将任何类型的对象转换为 JObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:检查 JObject 中的空或空 JToken 下一篇:使用 NewtonSoft 在一行中生成 JSON 对象

                  相关文章

                  最新文章

                • <small id='vHthv'></small><noframes id='vHthv'>

                • <tfoot id='vHthv'></tfoot>

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