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

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

    <legend id='QxP7n'><style id='QxP7n'><dir id='QxP7n'><q id='QxP7n'></q></dir></style></legend>

        使用 JSON.NET 反序列化其中值是字段名称的 JSON

        Deserialize the JSON where the values are field names with JSON.NET(使用 JSON.NET 反序列化其中值是字段名称的 JSON)

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

            <legend id='G9Gdb'><style id='G9Gdb'><dir id='G9Gdb'><q id='G9Gdb'></q></dir></style></legend>
          • <tfoot id='G9Gdb'></tfoot>
                <tbody id='G9Gdb'></tbody>

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

                1. 本文介绍了使用 JSON.NET 反序列化其中值是字段名称的 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个非常不受欢迎的情况,需要我反序列化 JSON,其中值是 JSON.NET 的字段名称.假设我有以下结构非常正确的 JSON:

                  I have a very undesirable situation which requires me to deserialize the JSON where the values are field names with JSON.NET. Assuming that I have the following JSON which is very properly structured:

                  {
                      "name": "tugberk",
                      "roles": [
                          { "id": "1", "name": "admin" },
                          { "id": "2", "name": "guest" }
                      ]
                  }
                  

                  使用 JSON.NET 将其反序列化为 CLR 对象非常容易:

                  It's very easy to deserialize this with JSON.NET to a CLR object:

                  class Program
                  {
                      static void Main(string[] args)
                      {
                          var camelCaseSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
                  
                          var text = File.ReadAllText("user_normal.txt");
                          var obj = JsonConvert.DeserializeObject<User>(text, camelCaseSettings);
                      }
                  }
                  
                  public class User
                  {
                      public string Name { get; set; }
                      public Role[] Roles { get; set; }
                  }
                  
                  public class Role
                  {
                      public int Id { get; set; }
                      public string Name { get; set; }
                  }
                  

                  但是,在我目前的情况下,我有以下可怕的 JSON,它在值方面等同于上述 JSON:

                  However, in my current case, I have the following horrible JSON which is equivalent to above JSON in terms of values:

                  {
                      "name": "tugberk",
                      "roles": {
                          "1": { "name": "admin" },
                          "2": { "name": "guest" }
                      }
                  }
                  

                  如您所见,roles 字段不是数组;它是一个对象,其中包含其他值作为对象,其唯一键作为字段名称(这太可怕了).使用 JSON.NET 将此 JSON 反序列化为上述 User 类的最佳方法是什么?

                  As you can see, roles field is not an array; it's an object which contains other values as objects with it's unique keys as their field names (which is horrible). What's the best way to deserialize this JSON to above User class with JSON.NET?

                  推荐答案

                  您可以创建一个自定义 JsonConverter 序列化/反序列化 Role[].然后,您可以使用 JsonConverterAttribute 装饰您的 Roles 属性,如下所示:

                  You can create a custom JsonConverter which serializes/deserializes Role[]. You can then decorate your Roles property with the JsonConverterAttribute like this:

                  public class User
                  {
                      public string Name { get; set; }
                      [JsonConverter(typeof(RolesConverter))]
                      public Role[] Roles { get; set; }
                  }
                  

                  在您的转换器类中,您可以读取一个对象并返回一个数组.您的转换器类可能如下所示:

                  In your converter class you are able to read an object and return an array instead. Your converter class may look like this:

                  class RolesConverter : JsonConverter
                  {
                      public override bool CanConvert(Type objectType)
                      {
                          return objectType == typeof(Role[]);
                      }
                  
                      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                      {
                          // deserialize as object
                          var roles = serializer.Deserialize<JObject>(reader);
                          var result = new List<Role>();
                  
                          // create an array out of the properties
                          foreach (JProperty property in roles.Properties())
                          {
                              var role = property.Value.ToObject<Role>();
                              role.Id = int.Parse(property.Name);
                              result.Add(role);
                          }
                  
                          return result.ToArray();
                      }
                  
                  
                      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                      {
                          throw new NotImplementedException();
                      }
                  }
                  

                  这篇关于使用 JSON.NET 反序列化其中值是字段名称的 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)
                2. <legend id='oQ83e'><style id='oQ83e'><dir id='oQ83e'><q id='oQ83e'></q></dir></style></legend>
                    <tbody id='oQ83e'></tbody>

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

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

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

                          <tfoot id='oQ83e'></tfoot>