在转换为json时,如何格式化对象中只有字符串的属性? [英] How to format properties that are only string in an object while converting to json?
问题描述
实例类型不清楚.我以 Foo 为例. 我有一个格式化方法和一个类似下面的类,
The intance type is not clear. I am using Foo as example. I have a format method and a class like below,
public string FormatMethod(string s){
//for example pattern ++
return "++" + s + "++";
}
public class Foo{
public int FooId {get;set;}
public string Name {get;set;}
public string Desciption {get;set;}
}
var foo = new Foo{ FooId = 1, Name = "FooName", Description = "Bla bla bla" };
// or
var list = new List<Foo>();
list.Add(foo);
var json = JsonConvert.SerializeObject(list);
//or
var jsonlist = JsonConvert.SerializeObject(foo);
我希望将对象或列表中字符串形式的属性在转换为json时发送给format方法,
I would like the properties that are string in an object or in a list to send to the format method while converting to json,
我希望json结果如下所示,
I would like json result to be like below,
json结果
{"FooId": 1 , "Name": "++FooName++", "Description" : "++Bla bla bla++" }
或作为列表
[{"FooId": 1 , "Name": "++FooName++", "Description" : "++Bla bla bla++" }]
我该怎么办?
编辑:
例如,我想在对象被序列化时应用任何模式 名称为"FooName",序列化后必须为"++ FooName ++".
I would like to apply any pattern while the object being serilalized, for example The name that is 'FooName', it need to be '++FooName++' after serialize.
我认为可以使用myconverter来完成,但是怎么办呢?
I think it can be done using myconverter, but how ?
例如:
public class MyConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// need to do something in here, I don't know what to do.
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
}
推荐答案
转换器:
class StringFormatConverter : JsonConverter
{
public string Format { get; set; }
public override void WriteJson (JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(string.Format(Format, value));
}
public override object ReadJson (JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
throw new NotSupportedException();
}
public override bool CanConvert (Type objectType)
{
return objectType == typeof(string);
}
}
用法:
Console.WriteLine(JsonConvert.SerializeObject(new List<Foo> {
new Foo { FooId = 1, Name = "FooName", Description = "Bla bla bla" }
}, new JsonSerializerSettings {
Converters = { new StringFormatConverter { Format = "++{0}++" } }
}));
输出:
[{"FooId":1,"Name":"++FooName++","Description":"++Bla bla bla++"}]
如果需要将字符串修改限制为特定属性,则可以使用JsonConverterAttribute
和JsonPropertyAttribute.ItemConverterType
(并从JsonSerializerSettings
中删除全局"转换器).
If you need to limit string modification to specific properties, you can use JsonConverterAttribute
and JsonPropertyAttribute.ItemConverterType
(and remove "global" converter from JsonSerializerSettings
).
这篇关于在转换为json时,如何格式化对象中只有字符串的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!