JSON.Net Seralize值没有引号 [英] JSON.Net Seralize values don't have quotation marks
问题描述
以下代码:
var json = JsonConvert.SerializeObject(fooObject,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
});
分散一个json(string)对象:
{
fooA:0,
fooB:0,
fooC:false,
fooD:false ,
fooE:0,
fooF:0,
}
值不在引号中,如fooA:0。这是我想要的行为。
是否有强制执行此行为的方法?
在JSON格式中,数字和布尔值在字符串周围没有引号,而字符串则是这样(参见 JSON.org 如果你想要引用所有的基元,你可以选择一些选项: 前两个选项是不言自明的。如果您选择使用转换器,代码可能如下所示: 要使用它,只需传递一个转换器实例到 演示: The following code: Seralizes a json (string) object of: {
"fooA":0,
"fooB":0,
"fooC":false,
"fooD":false,
"fooE":0,
"fooF":0,
} 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: The first two options are pretty self-explanatory. If you opted to go with a converter, the code might look something like this: To use it, simply pass an instance of the converter to the Demo: Output:
这篇关于JSON.Net Seralize值没有引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
string
。
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}
var json = JsonConvert.SerializeObject(fooObject,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
});
string
.Dictionary<string, string>
(or a DTO) and serialize that instead.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.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();
}
}
JsonConvert.SerializeObject
method.string json = JsonConvert.SerializeObject(foo, new PrimitiveToStringConverter());
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; }
}
{"Int":"6","Bool":"true","Float":"3.14159"}