所以,
我有一个问题序列化我的JSON对象到我的实体/模型对象。
我有以下代码片段:
var serializedValue = JsonConvert.SerializeObject(newRows, new MyModelConverter());
var csvToSave = JsonConvert.DeserializeObject<MyModel>(serializedValue);
其中newRows为List<KeyValuePair<string,string>>
其中实体看起来像这样:
public class MyModel: Entity
{
public int Id { get; set; } //primary key
public DateTime TouchTime { get; set; }
public DateTime InstallTime { get; set; }
public string EventName { get; set; }
}
回顾旧的StackOverflow帖子,我看到我需要使用自定义JsonConverter,以便我可以将我的数据转换为json对象。
这是我的转换器类:
public class MyModelConverter : JsonCreationConverter<MyModel>
{
protected override MyModel Create(Type objectType, JObject jObject)
{
return new MyModel();
}
}
public abstract class JsonCreationConverter<T> : JsonConverter
{
/// <summary>
/// Create an instance of objectType, based properties in the JSON object
/// </summary>
/// <param name="objectType">type of object expected</param>
/// <param name="jObject">
/// contents of JSON object that will be deserialized
/// </param>
/// <returns></returns>
protected abstract T Create(Type objectType, JObject jObject);
public override bool CanConvert(Type objectType)
{
return typeof(T).IsAssignableFrom(objectType);
}
public override object ReadJson(JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
// Load JObject from stream
JObject jObject = JObject.Load(reader);
// Create target object based on JObject
T target = Create(objectType, jObject);
// Populate the object properties
serializer.Populate(jObject.CreateReader(), target);
return target;
}
public override void WriteJson(JsonWriter writer,
object value,
JsonSerializer serializer)
{
List<KeyValuePair<string, object>> list = value as List<KeyValuePair<string, object>>;
writer.WriteStartArray();
foreach (var item in list)
{
writer.WriteStartObject();
writer.WritePropertyName(item.Key);
writer.WriteValue(item.Value);
writer.WriteEndObject();
}
writer.WriteEndArray();
}
}
但是当我运行程序时,我得到这个错误:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Data.Entities.Models.MyModel' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
我的Json对象现在是这样的:
[
{
"Key":"Attributed Touch Time",
"Value":"2016-09-06 20:11:50"
},
{
"Key":"Install Time",
"Value":"2016-09-06 20:14:12"
},
{
"Key":"Event Name",
"Value":"install"
}
]
然后我像这样固定了我的转换器:
public abstract class JsonCreationConverter<T> : JsonConverter
{
/// <summary>
/// Create an instance of objectType, based properties in the JSON object
/// </summary>
/// <param name="objectType">type of object expected</param>
/// <param name="jObject">
/// contents of JSON object that will be deserialized
/// </param>
/// <returns></returns>
protected abstract T Create(Type objectType, JObject jObject);
public override bool CanConvert(Type objectType)
{
return objectType == typeof(List<KeyValuePair<string, string>>);
}
public override object ReadJson(JsonReader reader,
Type objectType,
object existingValue,
JsonSerializer serializer)
{
// Load JObject from stream
JObject jObject = JObject.Load(reader);
// Create target object based on JObject
T target = Create(objectType, jObject);
// Populate the object properties
serializer.Populate(jObject.CreateReader(), target);
return target;
}
public override void WriteJson(JsonWriter writer,
object value,
JsonSerializer serializer)
{
List<KeyValuePair<string, string>> list = value as List<KeyValuePair<string, string>>;
writer.WriteStartArray();
foreach (var item in list)
{
writer.WriteStartObject();
writer.WritePropertyName(item.Key);
writer.WriteValue(item.Value);
writer.WriteEndObject();
}
writer.WriteEndArray();
}
}
但是我的Json对象现在看起来像这样:
[
{
"Attributed Touch Time":"2016-09-06 20:11:50"
},
{
"Install Time":"2016-09-06 20:14:12"
},
{
"Event Name":"install"
},
(etc...) //Not part of JSON, just cutting it short
]
这很好,但仍然不能映射到我的实体
看起来您正在为每个KVP创建一个新的JSON对象,而不是将KVP分组到单个对象中。将writer.WriteStartObject();
和writer.EndObject();
移到循环之外,您应该最终得到一个格式良好的JSON对象。
writer.WriteStartObject();
foreach (var item in list)
{
writer.WritePropertyName(item.Key);
writer.WriteValue(item.Value);
}
writer.WriteEndObject();
writer.WriteEndArray();
你需要把这些Start/End对象放到For循环之外。
所以你应该得到这个…
[
{
"Attributed Touch Time":"2016-09-06 20:11:50"
"Install Time":"2016-09-06 20:14:12"
"Event Name":"install"
},
]