繁体   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