[英]Deserializing complex Json objects
I want to deserialize a complex and let's say not well constructed json. 我想反序列化一个复杂的,并且说构造不好的json。 That code that I wrote doesn't deserialize the object, the
MovieInfo
property is null. 我编写的代码不会反序列化对象,
MovieInfo
属性为null。 You can find the example json in the code. 您可以在代码中找到示例json。 I want to avoid using
JObject.Parse
and dynamic
objects. 我想避免使用
JObject.Parse
和dynamic
对象。 What am I missing here? 我在这里想念什么?
using System.Collections.Generic;
using Newtonsoft.Json;
namespace ComplexJsonExample
{
class Program
{
static void Main(string[] args)
{
string jsonInText = @"
{
""movies"" : [
{
""Harry Potter"" : [
{ ""rating"": ""great""},
{ ""rating"": ""horrible""}
]
},
{
""Guardians of the galaxy"" : [
{ ""rating"": ""cool""},
{ ""rating"": ""awesome""}
]
}
]
}
";
var movieList = JsonConvert.DeserializeObject<MovieList>(jsonInText);
}
}
public class MovieList
{
[JsonProperty("movies")]
public IList<Movie> Movies { get; set; }
}
public class Movie
{
IDictionary<string, IList<MovieRating>> MovieInfo { get; set; }
}
public class MovieRating
{
[JsonProperty("rating")]
public string Rating { get; set; }
}
}
I consider this a bit of a hack, but it works, and deserializes your json string into the format you required: 我认为这有点hack,但是它可以正常工作,并将json字符串反序列化为所需的格式:
class MovieConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Movie);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var movie = new Movie(){
MovieInfo = new Dictionary<string,IList<MovieRating>>()
};
while (reader.Read() && reader.Value != null)
{
var name = (string)reader.Value;
reader.Read();
var ratings = ((JArray)serializer.Deserialize(reader)).Values<string>("rating");
movie.MovieInfo.Add(name, ratings.Select(r => new MovieRating(){ Rating = r}).ToList());
}
return movie;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
I did have to make two changes to your original objects, first I made the accessor for MovieInfo
public so I could access it, and second to add the JsonConverterAttribute
: 我确实必须对原始对象进行两项更改,首先我将
MovieInfo
访问MovieInfo
公开,以便可以访问它,其次添加JsonConverterAttribute
:
[JsonConverter(typeof(MovieConverter))]
public class Movie
{
public IDictionary<string, IList<MovieRating>> MovieInfo { get; set; }
}
It's a bit ugly, but you can do it like this: 这有点丑陋,但您可以这样做:
class MovieList
{
[JsonProperty("movies")]
public Movie[] Movies { get; set; }
}
class Movie : Dictionary<string, MovieRating[]>
{
}
class MovieRating
{
[JsonProperty("rating")]
public string Rating { get; set; }
}
It's weird that the movie is a dictionary with only one key (its title), but it matches the JSON structure. 奇怪的是,电影是只有一个键(其标题)的字典,但它与JSON结构匹配。 You can always map it to something more sensible after deserialization.
反序列化后,您始终可以将其映射到更明智的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.