2014-01-17 61 views
1

說例如我試圖將具有10個字段的對象轉換爲Json,但是我需要修改序列化這些字段1的過程。此刻,我不得不用手工寫出來的每一個屬性是這樣的:如何使用Json.net修改僅一個字段的Json序列化?

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
     writer.WriteStartObject(); 

     writer.WritePropertyName("Field1"); 
     serializer.Serialize(writer, value.Field1); 

     writer.WritePropertyName("Field2"); 
     serializer.Serialize(writer, value.Field2); 

     writer.WritePropertyName("Field3"); 
     serializer.Serialize(writer, value.Field3); 

     writer.WritePropertyName("Field4"); 
     serializer.Serialize(writer, Convert.ToInt32(value.Field4)); //Modifying one field here 

     //Six more times 

     writer.WriteEndObject(); 
    } 

這是不好的代碼,它的真正刺激有寫。有什麼辦法讓Json.net自動地序列化除一個屬性以外的所有屬性?或者可能自動生成一個JObject並修改它?

+0

你在找這個嗎? http://james.newtonking.com/json/help/index.html?topic=html/T_Newtonsoft_Json_JsonConvert.htm –

+0

不,因爲JsonConvert只需要獲取對象中的每個字段並對它們進行序列化。你可以使用[JsonIgnore]來跳過屬性,但這與你擁有的控件一樣多。 – Miguel

回答

3

您可以通過裝修物業嘗試,你需要用手工修改JsonConverterAttribute並通過適當的JsonConverter類型。

例如,使用OP的原始例如:

public class IntegerConverter : JsonConverter 
{ 
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
    { 
    serializer.Serialize(writer, Convert.ToInt32(value)); 
    } 

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) 
    { 
    throw new NotImplementedException(); 
    } 

    public override bool CanConvert(Type objectType) 
    { 
    return objectType == typeof(string); 
    } 
} 

class TestJson 
{ 
    public string Field1 { get; set; } 
    public string Field2 { get; set; } 
    public string Field3 { get; set; } 

    [JsonConverter(typeof(IntegerConverter))] 
    public string Field4 { get; set; }   
} 

可以使用JsonConvert然後序列化對象像往常一樣:

var test = new TestJson {Field1 = "1", Field2 = "2", Field3 = "3", Field4 = "4"}; 

var jsonString = JsonConvert.SerializeObject(test); 
1

如果您有權訪問該類(並且您始終需要以相同的方式對其進行序列化),則可以根據需要修改該類。假設this.is類:

public class MyClass 
{ 
    public string Value4 {get; set;} 
} 

如果你想值4被序列化作爲一個int你可以這樣做:

public class MyClass 
{ 
    [JsonIgnore()] 
    public string Value4 {get; set;} 

    public int Value4AsInt 
    { 
     return Convert.ToInt32(Value4); 
    } 
} 
1

您可以使用System.Reflection,但是它的速度慢,但你不必須修改類別

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) 
{ 
    writer.WriteStartObject(); 

    Type vType = value.GetType(); 
    MemberInfo[] properties = vType.GetProperties(BindingFlags.Public 
             | BindingFlags.Instance); 

    foreach (PropertyInfo property in properties) 
    { 
     object serValue = null; 
     if (property.Name == "Field4") 
     { 
      serValue = Convert.ToInt32(property.GetValue(value, null)); 
     } 
     else 
     { 
      serValue = property.GetValue(value, null); 
     } 
     writer.WritePropertyName(property.Name); 
     serializer.Serialize(writer, serValue); 
    } 

    writer.WriteEndObject(); 
}