<tfoot id='d1iOF'></tfoot>

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

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

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

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

      使用 Json.NET 对具有混合类型的数组进行序列化/反序列化

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

              <legend id='446rw'><style id='446rw'><dir id='446rw'><q id='446rw'></q></dir></style></legend>
                <tbody id='446rw'></tbody>

              <small id='446rw'></small><noframes id='446rw'>

                <tfoot id='446rw'></tfoot>

                本文介绍了使用 Json.NET 对具有混合类型的数组进行序列化/反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我正在尝试为我需要发送/接收的 JSON 编写一些 C# 包装类.在一个实例中,我收到了一个混合类型的多维数组:

                I'm trying to write some C# wrapper classes for JSON that I need to send/receive. In one instance, I'm receiving a multi-dimensional array of mixed types:

                stops: [
                    [0.1, "#55BF3B"],
                    [0.5, "#DDDF0D"],
                    [0.9, "#DF5353"]
                 ]
                

                如何在类属性中表示?

                [JsonProperty("stops")]
                public WhatType?[] Stops { get; set; }
                

                我真正想要的是一个像下面这样的类:

                What I'd really like is a class like the following:

                public class Stop
                {
                    public decimal Value { get; set; }
                    public string Color { get; set; }
                }
                

                这将序列化/反序列化为一个数组(没有属性名称):

                That would serialize/deserialize as an array (without property names):

                [.1, "#000000"]
                

                那么我的 Stops 属性可能只是一个 Stop 数组.但是,即使使用自定义转换器,我也不确定这是否可行.有什么想法吗?

                Then my Stops property could simply be an array of Stop. However, I'm not sure this is possible even with a custom converter. Any ideas?

                推荐答案

                是的,这几乎是一个教科书示例,说明您将使用 JsonConverter.
                下面是代码的样子:

                Yes, this is pretty much a textbook example of a situation where you would use a JsonConverter.
                Here is what the code would look like:

                class StopConverter : JsonConverter
                {
                    public override bool CanConvert(Type objectType)
                    {
                        return (objectType == typeof(Stop));
                    }
                
                    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                    {
                        JArray ja = JArray.Load(reader);
                        Stop stop = new Stop();
                        stop.Value = (decimal)ja[0];
                        stop.Color = (string)ja[1];
                        return stop;
                    }
                
                    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                    {
                        JArray ja = new JArray();
                        Stop stop = (Stop)value;
                        ja.Add(stop.Value);
                        ja.Add(stop.Color);
                        ja.WriteTo(writer);
                    }
                }
                

                要使用转换器,只需用 [JsonConverter] 属性装饰您的 Stop 类,如下所示:

                To use the converter, simply decorate your Stop class with a [JsonConverter] attribute like this:

                [JsonConverter(typeof(StopConverter))]
                public class Stop
                {
                    public decimal Value { get; set; }
                    public string Color { get; set; }
                }
                

                然后,一切都应该正常工作".这是一个往返演示:

                Then, everything should "just work". Here is a round-trip demo:

                class Program
                {
                    static void Main(string[] args)
                    {
                        string json = @"
                        { 
                            ""stops"": 
                            [
                                [0.1, ""#55BF3B""],
                                [0.5, ""#DDDF0D""],
                                [0.9, ""#DF5353""]
                            ]
                        }";
                
                        RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
                        foreach (Stop stop in obj.Stops)
                        {
                            Console.WriteLine("Value: " + stop.Value);
                            Console.WriteLine("Color: " + stop.Color);
                            Console.WriteLine();
                        }
                
                        json = JsonConvert.SerializeObject(obj, Formatting.Indented);
                        Console.WriteLine(json);
                    }
                }
                
                class RootObject
                {
                    [JsonProperty("stops")]
                    public List<Stop> Stops { get; set; }
                }
                
                [JsonConverter(typeof(StopConverter))]
                public class Stop
                {
                    public decimal Value { get; set; }
                    public string Color { get; set; }
                }
                

                输出:

                Value: 0.1
                Color: #55BF3B
                
                Value: 0.5
                Color: #DDDF0D
                
                Value: 0.9
                Color: #DF5353
                
                {
                  "stops": [
                    [
                      0.1,
                      "#55BF3B"
                    ],
                    [
                      0.5,
                      "#DDDF0D"
                    ],
                    [
                      0.9,
                      "#DF5353"
                    ]
                  ]
                }
                

                小提琴:https://dotnetfiddle.net/6XcY9r

                这篇关于使用 Json.NET 对具有混合类型的数组进行序列化/反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:序列化对象的自定义 $type 值 下一篇:使用 JSON.Net 对字典中的复杂类型进行特定用途的序列化

                相关文章

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

                  <tfoot id='MsneM'></tfoot>
                    <bdo id='MsneM'></bdo><ul id='MsneM'></ul>
                  1. <small id='MsneM'></small><noframes id='MsneM'>

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