Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JSON反序列化为C#对象-未反序列化任何数据_C#_Json_Serialization_Deserialization - Fatal编程技术网

将JSON反序列化为C#对象-未反序列化任何数据

将JSON反序列化为C#对象-未反序列化任何数据,c#,json,serialization,deserialization,C#,Json,Serialization,Deserialization,尝试将JSON字符串反序列化为C#对象时发现一个奇怪的问题。它“似乎”成功地执行了操作(因为它不会引发任何异常等),但是输出的POCO不包含JSON字符串中的任何数据,它只包含类型默认数据(null、“、0等)。我用其他JSON尝试过这个过程,效果很好 private string GetJson() { return @"{""backdrop_path"":"" / 1LrtAhWPSEetJLjblXvnaYtl7eA.jpg"",""created_by"

尝试将JSON字符串反序列化为C#对象时发现一个奇怪的问题。它“似乎”成功地执行了操作(因为它不会引发任何异常等),但是输出的POCO不包含JSON字符串中的任何数据,它只包含类型默认数据(null、“、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; }
}
私有字符串GetJson()
{
return“{”“background_path”“:”“/1LrtAhWPSEetJLjblXvnaYtl7eA.jpg”“创建人”“:[{”“id”“:488”“name”“:”“Steven Spielberg”“”“profile_path”“:”“/pok15unaw75bzj7bqo1ulehbpm.jpg”“,{”“id”“:31”“name”“:”“Tom Hanks”“,”“profile(path”“:”“”:”“/a14cnbytappyalapsglmfhilpeiw.jpg”“,“第1集运行时间”“:[60],“first”“air”“:”“2001:09”“genu-2009”“。”[{“id”:28,“name”:Action},{“id”:12,“name”:“冒险”},{“id”:18,“name”:“戏剧”},{“id”:10752,“name”:“War”},““主页”:”http://www.hbo.com/band-of-brothers“,”id“:4613“,”制作中“:false“,”语言“:[”de“,”fr“,”lt“,”nl“,”en“,”最后发布日期“:”2001-11-04“,”姓名“:”兄弟乐队“,”网络“:[“{”“id”“:49”“姓名”“:”“HBO”“}],”“剧集数”“:10”“季节数”“:1”“原籍国”“:[”“GB”“US”“],”“原语”“:”“en”“原名”“:”“兄弟乐队”“概览”“:”"根据对Easy Company幸存者的采访以及他们的日记和信件,兄弟乐队记录了这些人从格鲁吉亚伞兵训练到战争结束的经历根据斯蒂芬·安布罗斯(Stephen e.Ambrose)的同名著作《战争的恐怖》,安逸的人知道非凡的勇气和非凡的恐惧,并成为了传奇人物。(“,”流行“:3.435181“,”海报路径“:”/burt6oexd04imewqjo9oljrgua.jpg“,”制作公司“:[{”“名称:”“梦工厂SKG”,“id”“:27},{”“名称:”“HBO电影”,id:7429},{“名称”:“梦工厂电视台”,“id:15258}],“季节”:[{“播出日期”:null,“剧集计数”:4,“id”:14071,“海报路径”:“/bmn9iisadmajflrefcch0ttnyq.jpg”,“季节编号”:0},{“播出日期”:“2001-09-09”,“剧集计数”:10,“id”:14070,“海报路径”:“/158snovby2wztztg 1stczm1”,“剧集编号:”状态“:”结束“,”类型“:”脚本“,”投票平均“:8.5”,“投票计数“:47}”;
}
[测试方法]
public void反序列化MDBShowData_ValidShowData_ReturnDeserializedObject()
{
//安排
字符串jsonStream=GetJson();
JavaScriptSerializer jsonSerializer=新的JavaScriptSerializer();
//表演
var tmdbShowDetails=jsonSerializer.Deserialize(jsonStream);
//断言
Assert.IsNotNull(tmdbShowDetails);
Assert.IsNotNull(tmdbShowDetails.First().background\u路径);
}
公共类TmdbShowDetailsDto
{
公共字符串路径{get;set;}
public Created_By[]Created_By{get;set;}
public int[]事件运行时间{get;set;}
公共字符串first_air_date{get;set;}
公共类型[]类型{get;set;}
公共字符串主页{get;set;}
公共int id{get;set;}
公共布尔生成{get;set;}
公共字符串[]语言{get;set;}
公共字符串last_air_date{get;set;}
公共字符串名称{get;set;}
公共网络[]网络{get;set;}
公共整数集{get;set;}
公共整数{get;set;}
公共字符串[]来源国{get;set;}
公共字符串原始语言{get;set;}
公共字符串原始名称{get;set;}
公共字符串概述{get;set;}
公共浮点数{get;set;}
公共字符串poster_路径{get;set;}
公共制作公司[]制作公司{get;set;}
公共季节[]季节{get;set;}
公共字符串状态{get;set;}
公共字符串类型{get;set;}
公共浮动投票_平均值{get;set;}
公共整数投票计数{get;set;}
}
由创建的公共类
{
公共int id{get;set;}
公共字符串名称{get;set;}
公共字符串配置文件_路径{get;set;}
}
公共课体裁
{
公共int id{get;set;}
公共字符串名称{get;set;}
}
公共班级网络
{
公共int id{get;set;}
公共字符串名称{get;set;}
}
公营制作公司
{
公共字符串名称{get;set;}
公共int id{get;set;}
}
公共课季
{
公共字符串air_date{get;set;}
公共整数集_计数{get;set;}
公共int id{get;set;}
公共字符串poster_路径{get;set;}
公共整数{get;set;}
}
任何想法-我肯定我错过了一些显而易见的东西,但我看不到。使用.NET4.5

谢谢你的帮助

干杯

我用过,代码是这样的,所以你应该看看

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中使用粘贴特殊功能
粘贴JSON作为类

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搞混了。感谢那些指出类看起来是错误的人,让我重新检查了我的代码,并意识到这个问题的简单性。需要一些橡皮鸭来发现问题。干杯

谢谢你的回答-我确实使用了这个功能(之前使用过几次该功能,都很好)也许我是在装傻,今晚我会再试试。谢谢你的回复,我肯定我以前试过这条路线