Let's assume you have a json array similar to this:
[{"text":"0"},{"text":"1"}......]
I'll declare a class for the object type
public class TempClass
{
public string text;
}
Now, the deserializetion part
JsonSerializer ser = new JsonSerializer();
ser.Converters.Add(new DummyConverter<TempClass>(t =>
{
//A callback method
Console.WriteLine(t.text);
}));
ser.Deserialize(new JsonTextReader(new StreamReader(File.OpenRead(fName))),
typeof(List<TempClass>));
And a dummy JsonConverter class to intercept the deserialization
public class DummyConverter<T> : JsonConverter
{
Action<T> _action = null;
public DummyConverter(Action<T> action)
{
_action = action;
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(TempClass);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
serializer.Converters.Remove(this);
T item = serializer.Deserialize<T>(reader);
_action( item);
return null;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…