2015-11-20 85 views
2

發現嘗試反序列化JSON字符串到C#對象時,一個奇怪的問題。它似乎「成功」執行操作(因爲它不會拋出任何異常等),但是輸出的POCO包含的數據不包含任何來自JSON字符串的數據,它只包含類型默認數據(空值,「」,0等) )。我已經嘗試過與其他JSON這個過程,它工作正常。反序列化JSON到C#對象 - 沒有數據被反序列

private string GetJson() 
    { 
     return @"{""backdrop_path"":""/1LrtAhWPSEetJLjblXvnaYtl7eA.jpg"",""created_by"":[{""id"":488,""name"":""Steven Spielberg"",""profile_path"":""/pOK15UNaw75Bzj7BQO1ulehbPPm.jpg""},{""id"":31,""name"":""Tom Hanks"",""profile_path"":""/a14CNByTYALAPSGlwlmfHILpEIW.jpg""}],""episode_run_time"":[60],""first_air_date"":""2001 - 09 - 09"",""genres"":[{""id"":28,""name"":""Action""},{""id"":12,""name"":""Adventure""},{""id"":18,""name"":""Drama""},{""id"":10752,""name"":""War""}],""homepage"":""http://www.hbo.com/band-of-brothers"",""id"":4613,""in_production"":false,""languages"":[""de"",""fr"",""lt"",""nl"",""en""],""last_air_date"":""2001-11-04"",""name"":""Band of Brothers"",""networks"":[{""id"":49,""name"":""HBO""}],""number_of_episodes"":10,""number_of_seasons"":1,""origin_country"":[""GB"",""US""],""original_language"":""en"",""original_name"":""Band of Brothers"",""overview"":""Drawn from interviews with survivors of Easy Company, as well as their journals and letters, Band of Brothers chronicles the experiences of these men from paratrooper training in Georgia through the end of the war. As an elite rifle company parachuting into Normandy early on D-Day morning, participants in the Battle of the Bulge, and witness to the horrors of war, the men of Easy knew extraordinary bravery and extraordinary fear - and became the stuff of legend. Based on Stephen E. Ambrose's acclaimed book of the same name."",""popularity"":3.435181,""poster_path"":""/bUrt6oeXd04ImEwQjO9oLjRguaA.jpg"",""production_companies"":[{""name"":""DreamWorks SKG"",""id"":27},{""name"":""HBO Films"",""id"":7429},{""name"":""DreamWorks Television"",""id"":15258}],""seasons"":[{""air_date"":null,""episode_count"":4,""id"":14071,""poster_path"":""/bMN9iiSAdnmAjflREfCCH0TTNyQ.jpg"",""season_number"":0},{""air_date"":""2001-09-09"",""episode_count"":10,""id"":14070,""poster_path"":""/15SN18OVbYt12Wzttclh51Sz9m1.jpg"",""season_number"":1}],""status"":""Ended"",""type"":""Scripted"",""vote_average"":8.5,""vote_count"":47}"; 
    } 

    [TestMethod] 
    public void DeserializeTmdbShowData_ValidShowData_ReturnDeserializedObject() 
    { 
     //Arrange 
     string jsonStream = GetJson(); 
     JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); 

     //Act 
     var tmdbShowDetails = jsonSerializer.Deserialize<List<TmdbShowDetailsDto>>(jsonStream); 

     //Assert 
     Assert.IsNotNull(tmdbShowDetails); 
     Assert.IsNotNull(tmdbShowDetails.First().backdrop_path); 
    } 




public class TmdbShowDetailsDto 
{ 
    public string backdrop_path { get; set; } 
    public Created_By[] created_by { get; set; } 
    public int[] episode_run_time { get; set; } 
    public string first_air_date { get; set; } 
    public Genre[] genres { get; set; } 
    public string homepage { get; set; } 
    public int id { get; set; } 
    public bool in_production { get; set; } 
    public string[] languages { get; set; } 
    public string last_air_date { get; set; } 
    public string name { get; set; } 
    public Network[] networks { get; set; } 
    public int number_of_episodes { get; set; } 
    public int number_of_seasons { get; set; } 
    public string[] origin_country { get; set; } 
    public string original_language { get; set; } 
    public string original_name { get; set; } 
    public string overview { get; set; } 
    public float popularity { get; set; } 
    public string poster_path { get; set; } 
    public Production_Companies[] production_companies { get; set; } 
    public Season[] seasons { get; set; } 
    public string status { get; set; } 
    public string type { get; set; } 
    public float vote_average { get; set; } 
    public int vote_count { get; set; } 
} 

public class Created_By 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string profile_path { get; set; } 
} 

public class Genre 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
} 

public class Network 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
} 

public class Production_Companies 
{ 
    public string name { get; set; } 
    public int id { get; set; } 
} 

public class Season 
{ 
    public string air_date { get; set; } 
    public int episode_count { get; set; } 
    public int id { get; set; } 
    public string poster_path { get; set; } 
    public int season_number { get; set; } 
} 

任何想法 - 我確定我錯過了一些顯而易見的東西,但我看不到它。使用.NET 4.5

幫助表示讚賞。

乾杯

+0

我會通過一些發佈的答案來準備,並給予它的另一個嘗試,即時通訊相當肯定Im做一些愚蠢的 – ree6

回答

0

所以這個問題我不能在夜裏找出或清晨 - 我花了,直到今天下午,直到我意識到,我是有錯誤的JSON !!,所以難怪它不工作!我混合了一些提供JSON的不同apis。感謝那些指出類似錯誤的人,讓我仔細檢查我的代碼,並認識到這個問題的簡單性。有點需要一些橡膠來解決問題。乾杯

1

我用http://json2csharp.com/,代碼是這樣的,所以你應該看看它。

public class Poster 
{ 
    public string full { get; set; } 
    public string medium { get; set; } 
    public string thumb { get; set; } 
} 

public class Fanart 
{ 
    public string full { get; set; } 
    public string medium { get; set; } 
    public string thumb { get; set; } 
} 

public class Images 
{ 
    public Poster poster { get; set; } 
    public Fanart fanart { get; set; } 
} 

public class Ids 
{ 
    public int trakt { get; set; } 
    public string slug { get; set; } 
    public int tvdb { get; set; } 
    public string imdb { get; set; } 
    public int tmdb { get; set; } 
    public int tvrage { get; set; } 
} 

public class Show 
{ 
    public string title { get; set; } 
    public string overview { get; set; } 
    public int year { get; set; } 
    public string status { get; set; } 
    public Images images { get; set; } 
    public Ids ids { get; set; } 
} 

public class Poster2 
{ 
    public string full { get; set; } 
    public string medium { get; set; } 
    public string thumb { get; set; } 
} 

public class Fanart2 
{ 
    public string full { get; set; } 
    public string medium { get; set; } 
    public string thumb { get; set; } 
} 

public class Images2 
{ 
    public Poster2 poster { get; set; } 
    public Fanart2 fanart { get; set; } 
} 

public class Ids2 
{ 
    public int trakt { get; set; } 
    public string slug { get; set; } 
    public string imdb { get; set; } 
    public int tmdb { get; set; } 
} 

public class Movie 
{ 
    public string title { get; set; } 
    public string overview { get; set; } 
    public int year { get; set; } 
    public Images2 images { get; set; } 
    public Ids2 ids { get; set; } 
} 

public class Headshot 
{ 
    public object full { get; set; } 
    public object medium { get; set; } 
    public object thumb { get; set; } 
} 

public class Images3 
{ 
    public Headshot headshot { get; set; } 
} 

public class Ids3 
{ 
    public int trakt { get; set; } 
    public string slug { get; set; } 
    public string imdb { get; set; } 
    public int tmdb { get; set; } 
    public int tvrage { get; set; } 
} 

public class Person 
{ 
    public string name { get; set; } 
    public Images3 images { get; set; } 
    public Ids3 ids { get; set; } 
} 

public class RootObject 
{ 
    public string type { get; set; } 
    public object score { get; set; } 
    public Show show { get; set; } 
    public Movie movie { get; set; } 
    public Person person { get; set; } 
} 
+0

感謝您的答覆,我敢肯定,我想這條路之前 - 我給它一個去。再次感謝 – ree6

0

您可以使用粘貼 - 特殊功能在您的VS-IDE Paster JSON as Classes

public class Rootobject 
{ 
    public Class1[] Property1 { get; set; } 
} 

public class Class1 
{ 
    public string type { get; set; } 
    public object score { get; set; } 
    public Show show { get; set; } 
    public Movie movie { get; set; } 
    public Person person { get; set; } 
} 

public class Show 
{ 
    public string title { get; set; } 
    public string overview { get; set; } 
    public int year { get; set; } 
    public string status { get; set; } 
    public Images images { get; set; } 
    public Ids ids { get; set; } 
} 

public class Images 
{ 
    public Poster poster { get; set; } 
    public Fanart fanart { get; set; } 
} 

public class Poster 
{ 
    public string full { get; set; } 
    public string medium { get; set; } 
    public string thumb { get; set; } 
} 

public class Fanart 
{ 
    public string full { get; set; } 
    public string medium { get; set; } 
    public string thumb { get; set; } 
} 

public class Ids 
{ 
    public int trakt { get; set; } 
    public string slug { get; set; } 
    public int tvdb { get; set; } 
    public string imdb { get; set; } 
    public int tmdb { get; set; } 
    public int tvrage { get; set; } 
} 

public class Movie 
{ 
    public string title { get; set; } 
    public string overview { get; set; } 
    public int year { get; set; } 
    public Images1 images { get; set; } 
    public Ids1 ids { get; set; } 
} 

public class Images1 
{ 
    public Poster1 poster { get; set; } 
    public Fanart1 fanart { get; set; } 
} 

public class Poster1 
{ 
    public string full { get; set; } 
    public string medium { get; set; } 
    public string thumb { get; set; } 
} 

public class Fanart1 
{ 
    public string full { get; set; } 
    public string medium { get; set; } 
    public string thumb { get; set; } 
} 

public class Ids1 
{ 
    public int trakt { get; set; } 
    public string slug { get; set; } 
    public string imdb { get; set; } 
    public int tmdb { get; set; } 
} 

public class Person 
{ 
    public string name { get; set; } 
    public Images2 images { get; set; } 
    public Ids2 ids { get; set; } 
} 

public class Images2 
{ 
    public Headshot headshot { get; set; } 
} 

public class Headshot 
{ 
    public object full { get; set; } 
    public object medium { get; set; } 
    public object thumb { get; set; } 
} 

public class Ids2 
{ 
    public int trakt { get; set; } 
    public string slug { get; set; } 
    public string imdb { get; set; } 
    public int tmdb { get; set; } 
    public int tvrage { get; set; } 
} 
+0

感謝您的回覆 - 我確實使用了此功能(並且在使用該功能之前幾次都沒​​有問題)。也許我正在愚蠢,生病在這個夜晚再次旋轉。 – ree6

0

看起來像你的JSON字符串是不是在您嘗試反序列化正確的格式。例如,我沒有在C#類中找到show屬性,但它在JSON中存在。

+0

你是對的,錯了JSON – ree6