ためにJSON DeserializeObjectの変換を実行している:は、次のようなJSON文字列を想定すると、プロパティ
string json = '{"string_property":"foo_bar", ... other objects here ...}';
解析されたオブジェクトに変換を実行するための方法がある場合、私は思っていた代わりにfoo_bar
を得ることのように、私は手動でデシリアライズ後に私のPOCOを変更しますが、どうなるかと思ったことができ
public string Transform(string s) {
return s.Replace("_"," ");
}
(本当に何もすることができます)は、以下の方法を実行した後foo bar
を取得します"クリーナー"アプローチ?それはように使用後
public class ReplacingStringConverter : JsonConverter
{
readonly string oldValue;
readonly string newValue;
public ReplacingStringConverter(string oldValue, string newValue)
{
if (oldValue == null)
throw new ArgumentNullException("oldValue");
if (string.IsNullOrEmpty(oldValue))
throw new ArgumentException("string.IsNullOrEmpty(oldValue)");
this.oldValue = oldValue;
this.newValue = newValue;
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(string);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
var s = (string)JToken.Load(reader);
return s.Replace(oldValue, newValue);
}
public override bool CanWrite { get { return false; } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
:あなたはすべての文字列型の値をターゲットにcustom JsonConverter
を使用して、ルートオブジェクトをデシリアライズするよう
ルックhttp://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm –
で:
はその後、単純にデコードする必要がある、次のPOCOのプロパティを追加しますプロパティ名や値だけを変換しようとしていますか?そして、すべてのプロパティに対して同じ変換が必要なのですか? –プロパティ名を変更する必要がなく、すべての文字列プロパティに対して同じ変換が必要な場合は、最初に逆シリアル化してから、すべての文字列プロパティの値を変換します。 –