我有一种情况,从 REST
服务返回的 JSON
返回一个电影对象列表,所有这些都包含大量信息.REST
服务结果中的几个字段会根据可用信息而变化.
I have a situation where the JSON
returned from a REST
-service returns a list of Movie-objects, all specced out with a ton of information. A couple of fields in that REST
-service result changes depending on the information available.
一个例子:一部电影总是有一些屏幕截图(图像)、演员和导演.根据所讨论的电影,可能有一个或多个图像、一个或多个演员和一个或多个导演.几个案例的示例 JSON:
An example: A Movie always has some screen captures (images), actors and directors. Depending on the movie in question, there might be one or more images, one or more actors and one or more directors. Sample JSON for a couple of cases:
{
"title": "Movie title",
"images": [
"http://www.url.com/img_0.jpg",
"http://www.url.com/img_1.jpg",
"http://www.url.com/img_2.jpg",
"http://www.url.com/img_3.jpg",
"http://www.url.com/img_4.jpg"
],
"actors": [
"Steven Berkoff",
"Nikolaj Coster-Waldau",
"Julie Cox"
],
"directors": "Simon Aeby"
},
{
"title": "Another movie",
"images": "http://www.url.com/img_1.jpg",
"actors": "actor 1"
"directors": [
"Justin Bieber",
"Justin Timberlake"
]
}
问题是,使用 JSON.net,我如何创建一个转换器来处理这个问题?我一直在网上搜索,但仍然没有找到解决方案.
The question is, using JSON.net, how can I create a converter that deals with this problem? I've been scouring the internet, but still haven't found a solution.
关于同一个问题的另一个旋转:如果一个字段是字符串列表或简单字符串,我如何让 JSON.NET 以任何一种方式创建一个列表(如果只是一个简单的字符串,则创建一个包含一个成员的列表)
Another spin on the same question: If a field is either a List of strings or a simple string, how do I make JSON.NET create a List either way (and if just a simple string, create a list with one member)
这个 REST 服务不在我的控制范围内
This REST-service is out of my control
好吧,我这样做是为了好玩,但无论如何,我认为这不是有用或最好的方法......
Ok, I did it for fun, but don't think is useful or the best way, anyway...
将动态"属性声明为对象,然后创建获取属性的方法,例如 ImagesAsList 或 ImagesAsString.我用扩展方法做到了.....
Declaring the "dynamic" attributes as object and then create methods to obtain the properties as something like ImagesAsList or ImagesAsString. I did it with Extension Methods.....
var movies = JsonConvert.DeserializeObject<List<Movie>>(str);
类
class Movie
{
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("images")]
public object Images { get; set; }
[JsonProperty("actors")]
public object Actor { get; set; }
[JsonProperty("directors")]
public object Directors { get; set; }
}
扩展方法
static class MovieExtension
{
public static List<string> ImagesAsList(this Movie m)
{
var jArray = (m.Images as JArray);
if (jArray == null) return null;
return jArray.Select(x => x.ToString()).ToList();
}
public static string ImagesAsString(this Movie m)
{
return m.Images as string;
}
}
编辑
阅读@yamen 评论后,我做了一些更改,例如:
After reading @yamen comments I did some changes like:
var settings = new JsonSerializerSettings();
settings.Converters.Add(new MoviesConverter());
var movies = JsonConvert.DeserializeObject<List<Movie>>(str, settings);
类
class Movie
{
[JsonProperty("title")]
public List<string> Title { get; set; }
[JsonProperty("images")]
public List<string> Images { get; set; }
[JsonProperty("actors")]
public List<string> Actor { get; set; }
[JsonProperty("directors")]
public List<string> Directors { get; set; }
}
转换器
class MoviesConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(string)) || (objectType == typeof(List<string>)) ;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartArray)
{
var l = new List<string>();
reader.Read();
while (reader.TokenType != JsonToken.EndArray)
{
l.Add(reader.Value as string);
reader.Read();
}
return l;
}
else
{
return new List<string> { reader.Value as string };
}
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
//ToDo here we can decide to write the json as
//if only has one attribute output as string if it has more output as list
}
}
这篇关于JSON.net - 字段是字符串或列表<字符串>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!