在簡單值上使用 JsonConverter
使用 JsonCoverter 將執行時屬性從 api 響應反序列化為 Movies 模型中的 Timespan 物件的示例
JSON( http://www.omdbapi.com/?i=tt1663662)
{
Title: "Pacific Rim",
Year: "2013",
Rated: "PG-13",
Released: "12 Jul 2013",
Runtime: "131 min",
Genre: "Action, Adventure, Sci-Fi",
Director: "Guillermo del Toro",
Writer: "Travis Beacham (screenplay), Guillermo del Toro (screenplay), Travis Beacham (story)",
Actors: "Charlie Hunnam, Diego Klattenhoff, Idris Elba, Rinko Kikuchi",
Plot: "As a war between humankind and monstrous sea creatures wages on, a former pilot and a trainee are paired up to drive a seemingly obsolete special weapon in a desperate effort to save the world from the apocalypse.",
Language: "English, Japanese, Cantonese, Mandarin",
Country: "USA",
Awards: "Nominated for 1 BAFTA Film Award. Another 6 wins & 46 nominations.",
Poster: "https://images-na.ssl-images-amazon.com/images/M/MV5BMTY3MTI5NjQ4Nl5BMl5BanBnXkFtZTcwOTU1OTU0OQ@@._V1_SX300.jpg",
Ratings: [{
Source: "Internet Movie Database",
Value: "7.0/10"
},
{
Source: "Rotten Tomatoes",
Value: "71%"
},
{
Source: "Metacritic",
Value: "64/100"
}
],
Metascore: "64",
imdbRating: "7.0",
imdbVotes: "398,198",
imdbID: "tt1663662",
Type: "movie",
DVD: "15 Oct 2013",
BoxOffice: "$101,785,482.00",
Production: "Warner Bros. Pictures",
Website: "http://pacificrimmovie.com",
Response: "True"
}
電影模型
using Project.Serializers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
namespace Project.Models
{
[DataContract]
public class Movie
{
public Movie() { }
[DataMember]
public int Id { get; set; }
[DataMember]
public string ImdbId { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public DateTime Released { get; set; }
[DataMember]
[JsonConverter(typeof(RuntimeSerializer))]
public TimeSpan Runtime { get; set; }
}
}
RuntimeSerializer
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Project.Serializers
{
public class RuntimeSerializer : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(TimeSpan);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
JToken jt = JToken.Load(reader);
String value = jt.Value<String>();
Regex rx = new Regex("(\\s*)min$");
value = rx.Replace(value, (m) => "");
int timespanMin;
if(!Int32.TryParse(value, out timespanMin))
{
throw new NotSupportedException();
}
return new TimeSpan(0, timespanMin, 0);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
}
}
呼叫它
Movie m = JsonConvert.DeserializeObject<Movie>(apiResponse));