Json.NET:将嵌套数组反序列化为强类型对象 [英] Json.NET: deserialize nested arrays into strongly typed object
问题描述
我正在编写客户端应用程序,该应用程序应该处理服务器响应.响应使用JSON.我决定使用Json.NET对它们进行反序列化.而且我无法(在服务器端)简化或修改这些响应.这种特定的JSON响应的特别困难在于,不同的对象类型位于同一数组中:哈希和文件数组.因此,我想直接将此数组反序列化为强类型对象,而不是对象数组.我认为使用CustomCreationConverter
应该可以实现,但是我不知道如何实现.
I'm writing client application, which should process server responses. Responses are in JSON. I decided to use Json.NET to deserialize them. And I couldn't simplify or modify those responses (on server side). Particular difficulties of this specific JSON response is that different object types is in same array: hash and array of files. So, I would like to deserialize this array straight ahead into strongly typed object, rather than array of objects. I think that it should be possible to achieve with CustomCreationConverter
, but I couldn't figure how.
JSON:
{
"files":
[
"hash string",
[
["first file name", 12],
["second file name", 34]
]
]
}
我要实现的对象结构:
public class RootObject
{
[JsonProperty("files")]
public FilesContainer Container
{
get;
set;
}
}
public class FilesContainer
{
public string Hash
{
get;
set;
}
public File[] Files
{
get;
set;
}
}
[JsonConverter(typeof(FileJsonConverter))]
public class File
{
public string Name
{
get;
set;
}
public int Size
{
get;
set;
}
}
class FileJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
File obj = new File
{
Name = reader.ReadAsString(),
Size = reader.ReadAsInt32().GetValueOrDefault()
};
reader.Read();
return obj;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
推荐答案
我终于弄清楚了如何做到这一点.我需要在JsonConverter
内使用serializer.Deserialize
.因此,这是一个有效的示例.
I finally figured out how to do this. I was needed to use serializer.Deserialize
inside of JsonConverter
. So, here is a working example.
class Program
{
static string json = @"{
""files"":
[
""hash string"",
[
[""first file name"", 12],
[""second file name"", 34]
]
]
}";
static void Main(string[] args)
{
RootObject container = JsonConvert.DeserializeObject<RootObject>(json);
}
}
public class RootObject
{
[JsonProperty("files")]
public FilesContainer Container
{
get;
set;
}
}
[JsonConverter(typeof(FilesContainerJsonConverter))]
public class FilesContainer
{
public string Hash
{
get;
set;
}
public File[] Files
{
get;
set;
}
}
public class FilesContainerJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
FilesContainer obj = new FilesContainer();
obj.Hash = reader.ReadAsString();
reader.Read(); // to StartArray
obj.Files = serializer.Deserialize<File[]>(reader);
reader.Read(); // to EndArray
return obj;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
[JsonConverter(typeof(FileJsonConverter))]
public class File
{
public string Name
{
get;
set;
}
public int Size
{
get;
set;
}
}
class FileJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
File obj = new File
{
Name = reader.ReadAsString(),
Size = reader.ReadAsInt32().GetValueOrDefault()
};
reader.Read(); // to EndArray
return obj;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
这篇关于Json.NET:将嵌套数组反序列化为强类型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!