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

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

        <tfoot id='iZOnI'></tfoot>

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

        c# JSON序列化使用值而不是属性名

        时间:2023-05-23
        <legend id='DQQaD'><style id='DQQaD'><dir id='DQQaD'><q id='DQQaD'></q></dir></style></legend>

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

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

                  本文介绍了c# JSON序列化使用值而不是属性名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在开发一个 JSON 驱动的项目,我想为 SessionManager 对象提供一个动态的权限列表.虽然我可以使用一组键值对来获取权限,但我想知道是否可以删除属性名称,以便 keyPermission 值和 valueIsAllowed 值.

                  I am working on a JSON driven project and I would like to provide the SessionManager object with a dynamic list of permissionst. While I can work with an array of key value pairs for permissions, I was wondering if I could remove the property names so that the key is the Permission value and the value is the IsAllowed value.

                  public class SessionPermission
                  {
                      public string Permission { get; set; }
                      public bool IsAllowed { get; set; }
                  }
                  
                  
                  public class SessionManager
                  {
                      public string UserName { get; set; }
                      public string Password { get; set; }
                      public List<SessionPermission> Permissions { get; set; }
                  
                      public void SetPermissions()
                  
                      {
                          Permissions = new List<SessionPermission>
                          {
                              new SessionPermission {Permission = "CreateUsers", IsAllowed = false},
                              new SessionPermission {Permission = "EditUsers", IsAllowed = false},
                              new SessionPermission {Permission = "EditBlog", IsAllowed = true}
                          };
                      }
                  }
                  

                  当我生成 JSON 时,它会输出一组权限:

                  When I generate JSON it outputs an array of permissions:

                  {
                      "Permission": "CreateUsers",
                      "IsAllowed": false
                  },
                  

                  我想知道如何覆盖序列化,以便它使用值而不是属性名称.

                  I would like to know how to override the serialization so that it uses the values instead of the property names.

                  {
                      "CreateUsers": false
                  },
                  

                  推荐答案

                  您可以使用以下自定义转换器:

                  You could use the following custom converter:

                  public class SessionPermissionConverter : JsonConverter
                  {
                      public override object ReadJson(
                          JsonReader reader,
                          Type objectType,
                          object existingValue,
                          JsonSerializer serializer)
                      {
                          var obj = (JObject)JObject.ReadFrom(reader);
                  
                  
                          JProperty property = obj.Properties().FirstOrDefault();
                  
                          return new SessionPermission
                          {
                              Permission = property.Name,
                              IsAllowed = property.Value.Value<bool>()
                          };
                      }
                  
                      public override void WriteJson(
                          JsonWriter writer,
                          object value,
                          JsonSerializer serializer)
                      {
                         SessionPermission permission = (SessionPermission)value;
                  
                         JObject obj = new JObject();
                  
                         obj[permission.Permission] = permission.IsAllowed;
                  
                         obj.WriteTo(writer);
                      }
                  
                      public override bool CanConvert(Type t)
                      {
                          return typeof(SessionPermission).IsAssignableFrom(t);
                      }
                  
                      public override bool CanRead
                      {
                          get { return true; }
                      }
                  }
                  

                  用法:

                  var manager = new SessionManager();
                  manager.SetPermissions();
                  
                  string json = JsonConvert.SerializeObject(manager, new SessionPermissionConverter());
                  

                  示例 JSON:

                  {
                    "UserName": null,
                    "Password": null,
                    "Permissions": [
                      {
                        "CreateUsers": false
                      },
                      {
                        "EditUsers": false
                      },
                      {
                        "EditBlog": true
                      }
                    ]
                  }
                  

                  反之亦然.

                  示例: https://dotnetfiddle.net/mfbnuk

                  这篇关于c# JSON序列化使用值而不是属性名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 JSON.Net 对字典中的复杂类型进行特定用途的序列化 下一篇:JSON.Net 自定义合约序列化和集合

                  相关文章

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

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

                  <tfoot id='EjYUr'></tfoot>

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