简体   繁体   中英

Deserialize JSON to C# Object - No Data being deserialized

Found an odd issue when trying to deserialize a JSON string into a C# Object. It "seems" to perform the operation successfully (as in it does not throw any exception etc), however the outputted POCO contains does not contain any data from the JSON string, it only contains type default data (nulls,"", 0 etc). I have tried this process with other JSON and it works fine.

    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; }
}

Any ideas - Im sure I have missed something glaringly obvious but I cannot see it. Using .NET 4.5

Help appreciated.

Cheers

I used http://json2csharp.com/ , and the code is like this, so you should look at it.

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; }
}

You can use Paste-Special Feature in your 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; }
}

Look like you JSON string is not in correct format that you are trying to deserialize. For example i have not find show property in the C# class but it present in JSON.

So this issue I couldn't figure out in the night or early in the morning - it took me till this afternoon until I realised that I was had the wrong JSON!!, so no wonder it wasn't working! I got mixed up with a few different apis that provided JSON. Thanks to those who had pointed out that the class looks wrong, made me double check my code and realise the simplicity of this issue. Kinda needed some rubber ducking to spot the problem. Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM