2

实例类型不清楚。我以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++”。

我认为可以使用 myconverter 来完成,但是如何?

例如:

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();
        }
    } 
4

2 回答 2

1

转换器:

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++"}]

如果您需要将字符串修改限制为特定属性,您可以使用JsonConverterAttributeand JsonPropertyAttribute.ItemConverterType(并从 中删除“全局”转换器JsonSerializerSettings)。

于 2013-04-07T00:53:17.613 回答
0

这样做的正确方法可能是

  1. 反序列化
  2. 做你的搅拌操作
  3. 重新序列化

像这样

// 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);
于 2013-04-04T19:49:17.290 回答