영화 페이지 소스를 스크랩하여 IMDb 영화 정보를 얻는 앱을 작성합니다. 페이지 소스의 일부 영화 데이터는 'Schema.org'의 영화 스키마가 포함 된 JSON 형식입니다.
{
"@context": "http://schema.org",
"@type": "Movie",
"url": "/title/tt7131622/",
"name": "Once Upon a Time... in Hollywood",
"genre": [
"Comedy",
"Drama"
],
"actor": [
{
"@type": "Person",
"url": "/name/nm0000138/",
"name": "Leonardo DiCaprio"
},
{
"@type": "Person",
"url": "/name/nm0000093/",
"name": "Brad Pitt"
},
{
"@type": "Person",
"url": "/name/nm3053338/",
"name": "Margot Robbie"
},
{
"@type": "Person",
"url": "/name/nm0386472/",
"name": "Emile Hirsch"
}
],
"director": {
"@type": "Person",
"url": "/name/nm0000233/",
"name": "Quentin Tarantino"
},
"creator": [
{
"@type": "Person",
"url": "/name/nm0000233/",
"name": "Quentin Tarantino"
},
{
"@type": "Organization",
"url": "/company/co0050868/"
},
{
"@type": "Organization",
"url": "/company/co0452101/"
},
{
"@type": "Organization",
"url": "/company/co0159772/"
}
}
JSON 개체를 역 직렬화하기 위해 "Movie"클래스를 만들었습니다. Person
이름이 "Director"인 속성 클래스가 있습니다.
internal class ImdbJsonMovie
{
public string Url { get; set; }
public string Name { get; set; }
public string Image { get; set; }
public List<string> Genre { get; set; }
public List<ImdbJsonPerson> Actor { get; set; }
public ImdbJsonPerson Director { get; set; }
//public string[] Creator { get; set; }
}
괜찮아. 그러나 문제는 "매트릭스"와 같은 일부 영화에 감독이 두 명 이상 있다는 것입니다.
{
"@context": "http://schema.org",
"@type": "Movie",
"url": "/title/tt0133093/",
"name": "The Matrix",
"genre": [
"Action",
"Sci-Fi"
],
"actor": [
{
"@type": "Person",
"url": "/name/nm0000206/",
"name": "Keanu Reeves"
},
{
"@type": "Person",
"url": "/name/nm0000401/",
"name": "Laurence Fishburne"
},
{
"@type": "Person",
"url": "/name/nm0005251/",
"name": "Carrie-Anne Moss"
},
{
"@type": "Person",
"url": "/name/nm0915989/",
"name": "Hugo Weaving"
}
],
"director": [
{
"@type": "Person",
"url": "/name/nm0905154/",
"name": "Lana Wachowski"
},
{
"@type": "Person",
"url": "/name/nm0905152/",
"name": "Lilly Wachowski"
}
],
"creator": [
{
"@type": "Person",
"url": "/name/nm0905152/",
"name": "Lilly Wachowski"
},
{
"@type": "Person",
"url": "/name/nm0905154/",
"name": "Lana Wachowski"
},
{
"@type": "Organization",
"url": "/company/co0002663/"
},
{
"@type": "Organization",
"url": "/company/co0108864/"
},
{
"@type": "Organization",
"url": "/company/co0060075/"
},
{
"@type": "Organization",
"url": "/company/co0019968/"
},
{
"@type": "Organization",
"url": "/company/co0070636/"
}
}
따라서 List<Person>
.
internal class ImdbJsonMovie
{
public string Url { get; set; }
public string Name { get; set; }
public string Image { get; set; }
public List<string> Genre { get; set; }
public List<ImdbJsonPerson> Actor { get; set; }
public List<ImdbJsonPerson> Director { get; set; }
//public string[] Creator { get; set; }
}
또 다른 문제는 Person
클래스와 Organization
클래스 가 만든 생성자 속성을 역 직렬화하는 방법 입니다.
그래서 질문은 "이 복잡한 JSON 객체를 역 직렬화하는 방법"입니다.
감사합니다
@Piotr 감사합니다. 완전히 작동했습니다. 답변의 첫 부분이 저에게 맞지 않았기 때문에 답변으로 다시 작성합니다.
이 링크에 정답이 있다고 말했듯이.
https://stackoverflow.com/questions/18994685/how-to-handle-both-a-single-item-and-an-array-for-the-same-property-using-json-n그래서이 수업을 만들었습니다.
class JsonConverter<T> : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(List<T>);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
if (token.Type == JTokenType.Array)
{
return token.ToObject<List<T>>();
}
return new List<T> { token.ToObject<T>() };
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
내 영화 수업을 이것으로 변경했습니다.
internal class ImdbJsonMovie
{
public string Url { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("image")]
public string Image { get; set; }
[JsonProperty("genre")]
[JsonConverter(typeof(JsonConverter<string>))]
public List<string> Genre { get; set; }
[JsonProperty("contentRating")]
public string ContentRating { get; set; }
[JsonProperty("actor")]
[JsonConverter(typeof(JsonConverter<ImdbJsonTypeEnum>))]
public List<ImdbJsonTypeEnum> Actor { get; set; }
[JsonProperty("director")]
[JsonConverter(typeof(JsonConverter<ImdbJsonTypeEnum>))]
public List<ImdbJsonTypeEnum> Director { get; set; }
[JsonProperty("creator")]
[JsonConverter(typeof(JsonConverter<ImdbJsonTypeEnum>))]
public List<ImdbJsonTypeEnum> Creator { get; set; }
}
그리고이 열거 형
public class ImdbJsonTypeEnum
{
[JsonProperty("@type")]
public TypeEnum Type { get; set; }
[JsonProperty("url")]
public string Url { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
public enum TypeEnum
{
Organization,
Person
};
}
한 명의 감독과 다중 감독 영화를 위해 일했습니다.
감사합니다
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다