本文介绍了JSON.Net Seralize值没有引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 以下代码: var json = JsonConvert.SerializeObject(fooObject, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, }); 分散一个json(string)对象: 值不在引号中,如fooA:0。这是我想要的行为。 是否有强制执行此行为的方法? 解决方案在JSON格式中,数字和布尔值在字符串周围没有引号,而字符串则是这样(参见 JSON.org 如果你想要引用所有的基元,你可以选择一些选项: 将您要序列化的对象的属性更改为 string 。 将您的值a Dictionary< string,string> (或DTO),然后序列化。 JsonConverter 来做转换。这个选项的优点是它可以全局应用,所以你不必改变所有的类。 前两个选项是不言自明的。如果您选择使用转换器,代码可能如下所示: pre $ class PrimitiveToStringConverter: public override bool CanConvert(Type objectType) { return objectType.IsPrimitive; $ b $ public void override WriteJson(JsonWriter writer,object value,JsonSerializer serializer) { writer.WriteValue(value.ToString()。ToLower()) ; } public override bool CanRead { get {return false; } $ b $ public override object ReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer) { throw new NotImplementedException(); code $ 要使用它,只需传递一个转换器实例到 JsonConvert.SerializeObject 方法。 string json = JsonConvert.SerializeObject (foo,new PrimitiveToStringConverter()); 演示: $ b $ { 6, Bool = true, Float = 3.14159 }; string json = JsonConvert.SerializeObject(foo,new PrimitiveToStringConverter()); Console.WriteLine(json); } } class Foo { public int Int {get;组; } public bool Bool {get;组; } public double Float {get;组;输出: pre> {Int:6,Bool:true,Float:3.14159} The following code:var json = JsonConvert.SerializeObject(fooObject, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, });Seralizes a json (string) object of:The values are not in quotation marks, such as "fooA":"0". This is the behavior that I want.Is there a way to enforce this behavior? 解决方案 In JSON format, numbers and booleans do not have quotes around them, while strings do (see JSON.org).If you want quotes around all your primitives, you have a few options:Change the properties of the object you are serializing to be of type string.Put your values into a Dictionary<string, string> (or a DTO) and serialize that instead.Make a custom JsonConverter to do the conversion. This option has the advantage that it can apply globally so that you don't have to change all your classes.The first two options are pretty self-explanatory. If you opted to go with a converter, the code might look something like this:class PrimitiveToStringConverter : JsonConverter{ public override bool CanConvert(Type objectType) { return objectType.IsPrimitive; } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteValue(value.ToString().ToLower()); } public override bool CanRead { get { return false; } } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); }}To use it, simply pass an instance of the converter to the JsonConvert.SerializeObject method.string json = JsonConvert.SerializeObject(foo, new PrimitiveToStringConverter());Demo:class Program{ static void Main(string[] args) { Foo foo = new Foo { Int = 6, Bool = true, Float = 3.14159 }; string json = JsonConvert.SerializeObject(foo, new PrimitiveToStringConverter()); Console.WriteLine(json); }}class Foo{ public int Int { get; set; } public bool Bool { get; set; } public double Float { get; set; }}Output:{"Int":"6","Bool":"true","Float":"3.14159"} 这篇关于JSON.Net Seralize值没有引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-23 15:06