c# - 将 JSON 反序列化为 C# 对象 - 没有数据被反序列化

标签 c# json serialization deserialization

尝试将 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; }
}

关于c# - 将 JSON 反序列化为 C# 对象 - 没有数据被反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33820551/

相关文章:

c# - Entity Framework 无法加载指定的元数据资源

javascript - 连接 python 和 javascript 进行双向通信

.net - 将 C# 对象序列化为 XML - 最快的方法?

python - 将 Unicode 对象转换为 Python Dict

c# - 如何在 csharp 中做嵌套的通用类(如果这是合适的名称)

c# - java解密和c#加密非法 key 大小问题

php - 如何将 JSON 从 PHP 传递到 jQuery

json - jq 加入公共(public)键

serialization - 为什么 MvvmCross 没有导航对象的内在序列化?

c# - 在 finally block 上使用未分配的局部变量