如何在对象中格式化仅为字符串的属性,同时转换为json
本文关键字:属性 json 转换 字符串 对象 格式化 | 更新日期: 2023-09-27 18:07:18
实例类型不清楚。我使用Foo为例。我有一个格式方法和一个类,如下所示
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,
我希望json结果如下,
json结果
{"FooId": 1 , "Name": "++FooName++", "Description" : "++Bla bla bla++" }
或作为列表
[{"FooId": 1 , "Name": "++FooName++", "Description" : "++Bla bla bla++" }]
我该怎么做?
编辑:
例如,我想在对象序列化时应用任何模式如果名称为'FooName',则序列化后需要为'++FooName++'。
我认为它可以使用我的转换器,但如何?
例如:
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
中删除"全局"转换器)。
正确的方法可能是
- <
- 反序列化/gh>
- 执行字符串操作
- re-serialize
一样
// build initial Json
var foo = new Foo { FooId = 1, Name = "FooName", Desciption = "Bla bla bla" };
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
string fooJson = json_serializer.Serialize(foo);
// change value in Json
Foo newFoo = json_serializer.Deserialize<Foo>(fooJson);
newFoo.Name = String.Format("++{0}++", newFoo.Name);
fooJson = json_serializer.Serialize(newFoo);
或者你想在转换为json之前格式化字符串,比如
Foo foo = new Foo { FooId = 1, Name = "FooName", Desciption = "Bla bla bla" };
Foo formattedFoo = new Foo {
FooId = foo.FooId,
Name = String.Format("++{0}++", foo.Name),
Desciption = String.Format("++{0}++", foo.Desciption)
};
JavaScriptSerializer json_serializer = new JavaScriptSerializer();
string fooJson = json_serializer.Serialize(formattedFoo);