簡體   English   中英

在轉換為json時,如何格式化對象中只有字符串的屬性?

[英]How to format properties that are only string in an object while converting to json?

實例類型不清楚。 我以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時發送給format方法,

我希望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();
        }
    } 

轉換器:

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

如果需要將字符串修改限制為特定屬性,則可以使用JsonConverterAttributeJsonPropertyAttribute.ItemConverterType (並從JsonSerializerSettings刪除“全局”轉換器)。

執行此操作的正確方法可能是

  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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM