簡體   English   中英

將 JSON 反序列化為 C# 對象 - 沒有數據被反序列化

[英]Deserialize JSON to C# Object - No Data being deserialized

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

    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

幫助表示贊賞。

干杯

我用的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; }
}

您可以在 VS-IDE Paster JSON as Classes使用 Paste-Special Feature 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; }
}

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

所以這個問題我在晚上或清晨無法弄清楚 - 直到今天下午我才意識到我有錯誤的 JSON !!,所以難怪它不起作用! 我混淆了一些提供 JSON 的不同 api。 感謝那些指出類看起來不對的人,讓我仔細檢查了我的代碼並意識到這個問題的簡單性。 有點需要一些橡皮擦來發現問題。 干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM