• <tfoot id='m1CEX'></tfoot>
    • <bdo id='m1CEX'></bdo><ul id='m1CEX'></ul>

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

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

      1. <legend id='m1CEX'><style id='m1CEX'><dir id='m1CEX'><q id='m1CEX'></q></dir></style></legend>

        Json.net 将数字属性序列化为字符串

        Json.net serialize numeric properties as string(Json.net 将数字属性序列化为字符串)

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

          <i id='X5L4Z'><tr id='X5L4Z'><dt id='X5L4Z'><q id='X5L4Z'><span id='X5L4Z'><b id='X5L4Z'><form id='X5L4Z'><ins id='X5L4Z'></ins><ul id='X5L4Z'></ul><sub id='X5L4Z'></sub></form><legend id='X5L4Z'></legend><bdo id='X5L4Z'><pre id='X5L4Z'><center id='X5L4Z'></center></pre></bdo></b><th id='X5L4Z'></th></span></q></dt></tr></i><div id='X5L4Z'><tfoot id='X5L4Z'></tfoot><dl id='X5L4Z'><fieldset id='X5L4Z'></fieldset></dl></div>
              <tfoot id='X5L4Z'></tfoot>
                <bdo id='X5L4Z'></bdo><ul id='X5L4Z'></ul>
                    <tbody id='X5L4Z'></tbody>
                  <legend id='X5L4Z'><style id='X5L4Z'><dir id='X5L4Z'><q id='X5L4Z'></q></dir></style></legend>
                  本文介绍了Json.net 将数字属性序列化为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 JsonConvert.SerializeObject 序列化模型对象.服务器期望所有字段都是字符串.我的模型对象具有数字属性和字符串属性.我无法向模型对象添加属性.有没有办法像字符串一样序列化所有属性值?我必须只支持序列化,不支持反序列化.

                  I am using JsonConvert.SerializeObject to serialize a model object. The server expects all fields as strings. My model object has numeric properties and string properties. I can not add attributes to the model object. Is there a way to serialize all property values as if they were strings? I have to support only serialization, not deserialization.

                  推荐答案

                  您可以提供自己的 JsonConverter,即使是数字类型.我刚刚尝试过,它可以工作 - 它又快又脏,而且您几乎肯定想扩展它以支持其他数字类型(longfloatdoubledecimal 等),但它应该可以帮助您:

                  You can provide your own JsonConverter even for numeric types. I've just tried this and it works - it's quick and dirty, and you almost certainly want to extend it to support other numeric types (long, float, double, decimal etc) but it should get you going:

                  using System;
                  using System.Globalization;
                  using Newtonsoft.Json;
                  
                  public class Model
                  {
                      public int Count { get; set; }
                      public string Text { get; set; }
                  
                  }
                  
                  internal sealed class FormatNumbersAsTextConverter : JsonConverter
                  {
                      public override bool CanRead => false;
                      public override bool CanWrite => true;
                      public override bool CanConvert(Type type) => type == typeof(int);
                  
                      public override void WriteJson(
                          JsonWriter writer, object value, JsonSerializer serializer)
                      {
                          int number = (int) value;
                          writer.WriteValue(number.ToString(CultureInfo.InvariantCulture));
                      }
                  
                      public override object ReadJson(
                          JsonReader reader, Type type, object existingValue, JsonSerializer serializer)
                      {
                          throw new NotSupportedException();
                      }
                  }
                  
                  class Program
                  {
                      static void Main(string[] args)
                      {
                          var model = new Model { Count = 10, Text = "hello" };
                          var settings = new JsonSerializerSettings
                          { 
                              Converters = { new FormatNumbersAsTextConverter() }
                          };
                          Console.WriteLine(JsonConvert.SerializeObject(model, settings));
                      }
                  }
                  

                  这篇关于Json.net 将数字属性序列化为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 自定义合约序列化和集合)

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

                      <tbody id='RBpag'></tbody>

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