我遇到一种情况,即我正在使用的 API 返回不一致的 JSON,我想使用 JSON.NET 对其进行反序列化。在一种情况下,它返回一个包含对象的对象(请注意,外部"1"可以是任何数字):
{
"1":{
"0":{
"db_id":"12835424",
"title":"XXX"
},
"1":{
"db_id":"12768978",
"title":"YYY"
},
"2":{
"db_id":"12768980",
"title":"ZZZ"
},
"3":{
"db_id":"12768981",
"title":"PPP"
}
}
}
在另一种情况下,它返回一个对象数组:
{
"3":[
{
"db_id":"12769199",
"title":"XXX"
},
{
"db_id":"12769200",
"title":"YYY"
},
{
"db_id":"12769202",
"title":"ZZZ"
},
{
"db_id":"12769243",
"title":"PPP"
}
]
}
我不知道为什么存在这种不一致,但这是我正在使用的格式。使用 JsonConvert.DeserializeObject
方法反序列化两种格式的正确方法是什么?
对于当前版本的 Json.NET(Json.NET 4.5 Release 11),这里有一个 CustomCreationConverter,它将处理有时作为对象反序列化,有时作为数组反序列化的 Json。
public class ObjectToArrayConverter<T> : CustomCreationConverter<List<T>> where T : new()
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
List<T> target = new List<T>();
try
{
// Load JObject from stream
JArray jArray = JArray.Load(reader);
// Populate the object properties
serializer.Populate(jArray.CreateReader(), target);
}
catch (JsonReaderException)
{
// Handle case when object is not an array...
// Load JObject from stream
JObject jObject = JObject.Load(reader);
// Create target object based on JObject
T t = new T();
// Populate the object properties
serializer.Populate(jObject.CreateReader(), t);
target.Add(t);
}
return target;
}
public override List<T> Create(Type objectType)
{
return new List<T>();
}
}
示例用法:
[JsonObject]
public class Project
{
[JsonProperty]
public string id { get; set; }
// The Json for this property sometimes comes in as an array of task objects,
// and sometimes it is just a single task object.
[JsonProperty]
[JsonConverter(typeof(ObjectToArrayConverter<Task>))]
public List<Task> tasks{ get; set; }
}
[JsonObject]
public class Task
{
[JsonProperty]
public string name { get; set; }
[JsonProperty]
public DateTime due { get; set; }
}
应该是通过创建JsonCreationConverter来实现的。这篇文章可能会有所帮助:http://dotnetbyexample.blogspot.nl/2012/02/json-deserialization-with-jsonnet-class.html