如何使用JsonProperty获取嵌套属性

 鲁小姐就不用围脖 发布于 2023-02-13 19:02

我正在尝试使用Json.net反序列化此JSON代码.它是关于特定电影的信息.一切都按预期工作,除了cast属性,它嵌套在credits属性中.我为我的班级中的演员制作了一个班级Actor和一个这个演员班的名单Movie.

这是JSON:

{
    "id":550,
    "original_title":"Fight Club",
    "credits":
    {
        "cast":
        [
            {
                "id":819,
                "name":"Edward Norton",
                "character":"The Narrator",
                "order":0,
                "cast_id":4,
                "profile_path":"/eIkFHNlfretLS1spAcIoihKUS62.jpg"
            },
            {
                "id":287,
                "name":"Brad Pitt",
                "character":"Tyler Durden",
                "order":1,
                "cast_id":5,
                "profile_path":"/kc3M04QQAuZ9woUvH3Ju5T7ZqG5.jpg"
            },
            {
                "id":1283,
                "name":"Helena Bonham Carter",
                "character":"Marla Singer",
                "order":2,
                "cast_id":6,
                "profile_path":"/58oJPFG1wefMC0Vj7sFzHPrm67J.jpg"
            },
            {
                "id":7470,
                "name":"Meat Loaf",
                "character":"Robert 'Bob' Paulson",
                "order":3,
                "cast_id":7,
                "profile_path":"/pwNyXgegO1nlZ8uWT847JM8EjGj.jpg"
            },
            {
                "id":7471,
                "name":"Zach Grenier",
                "character":"Richard Chesler",
                "order":4,
                "cast_id":8,
                "profile_path":"/jghYiKdNkVehKpiVyE97AWrU9KQ.jpg"
            },
            {
                "id":7472,
                "name":"Richmond Arquette",
                "character":"Intern",
                "order":5,
                "cast_id":9,
                "profile_path":null
            },
            {
                "id":7219,
                "name":"David Andrews",
                "character":"Thomas",
                "order":6,
                "cast_id":10,
                "profile_path":"/pxmxn29UHW9r6uvLrd7bEwLswlQ.jpg"
            },
            {
                "id":7473,
                "name":"Rachel Singer",
                "character":"Chloe",
                "order":11,
                "cast_id":11,
                "profile_path":null
            },
            {
                "id":7497,
                "name":"Holt McCallany",
                "character":"The Mechanic",
                "order":20,
                "cast_id":22,
                "profile_path":"/hQBfcw9KVszdenlTZTR8AIrSpex.jpg"
            },
            {
                "id":7498,
                "name":"Eion Bailey",
                "character":"Ricky",
                "order":22,
                "cast_id":23,
                "profile_path":"/4MnRgrwuiJvHsfoiJrIUL4TkfoC.jpg"
            },
            {
                "id":7499,
                "name":"Jared Leto",
                "character":"Angel Face",
                "order":23,
                "cast_id":24,
                "profile_path":"/lzQSuu5o0JC9mCncYjVnncSH5lO.jpg"
            },
            {
                "id":7500,
                "name":"Peter Iacangelo",
                "character":"Lou",
                "order":24,
                "cast_id":25,
                "profile_path":null
            },
            {
                "id":56112,
                "name":"David Lee Smith",
                "character":"Walter",
                "order":19,
                "cast_id":30,
                "profile_path":"/xYkMA9AWtUN93KV5hWzlDkcnebB.jpg"
            },
            {
                "id":1129738,
                "name":"George Maguire",
                "character":"Group Leader",
                "order":7,
                "cast_id":31,
                "profile_path":null
            },
            {
                "id":197790,
                "name":"Eugenie Bondurant",
                "character":"Weeping Woman",
                "order":8,
                "cast_id":32,
                "profile_path":null
            },
            {
                "id":68277,
                "name":"Christina Cabot",
                "character":"Group Leader",
                "order":9,
                "cast_id":33,
                "profile_path":"/7UBTv5lW6apPdVLnOqTTBMTJWwY.jpg"
            },
            {
                "id":202080,
                "name":"Sydney 'Big Dawg' Colston",
                "character":"Speaker",
                "order":10,
                "cast_id":34,
                "profile_path":null
            },
            {
                "id":1172435,
                "name":"Christie Cronenweth",
                "character":"Airline Attendant",
                "order":12,
                "cast_id":35,
                "profile_path":null
            },
            {
                "id":956719,
                "name":"Tim De Zarn",
                "character":"Inspector Bird",
                "order":13,
                "cast_id":36,
                "profile_path":null
            },
            {
                "id":59285,
                "name":"Ezra Buzzington",
                "character":"Inspector Dent",
                "order":14,
                "cast_id":37,
                "profile_path":"/dl0SIqpOqS05UpJHKuDQqZTwUvP.jpg"
            },
            {
                "id":1269622,
                "name":"Dierdre Downing-Jackson",
                "character":"Woman on Plane",
                "order":15,
                "cast_id":38,
                "profile_path":null
            },
            {
                "id":543293,
                "name":"Bob Stephenson",
                "character":"Airport Security Officer (as Robert J. Stephenson)",
                "order":16,
                "cast_id":39,
                "profile_path":"/iH86ZmoSGOCnWZ8aycavGdlu3ht.jpg"
            },
            {
                "id":62846,
                "name":"Charlie Dell",
                "character":"Doorman",
                "order":17,
                "cast_id":40,
                "profile_path":"/z0JgZxazJAVlHxLYXWM8eUZlOk8.jpg"
            },
            {
                "id":530040,
                "name":"Rob Lanza",
                "character":"Man in Suit",
                "order":18,
                "cast_id":41,
                "profile_path":null
            },
            {
                "id":137425,
                "name":"Joel Bissonnette",
                "character":"Food Court Maitre D'",
                "order":21,
                "cast_id":42,
                "profile_path":null
            },
            {
                "id":175120,
                "name":"Evan Mirand",
                "character":"Steph",
                "order":25,
                "cast_id":43,
                "profile_path":null
            },
            {
                "id":169636,
                "name":"Robby Robinson",
                "character":"Next Month's Opponent",
                "order":26,
                "cast_id":44,
                "profile_path":null
            },
            {
                "id":168924,
                "name":"Lou Beatty Jr.",
                "character":"Cop at Marla's Building",
                "order":27,
                "cast_id":45,
                "profile_path":null
            },
            {
                "id":1219497,
                "name":"Thom Gossom Jr.",
                "character":"Detective Stern",
                "order":28,
                "cast_id":46,
                "profile_path":null
            },
            {
                "id":157938,
                "name":"Valerie Bickford",
                "character":"Susan, Cosmetics Dealer",
                "order":29,
                "cast_id":47,
                "profile_path":null
            },
            {
                "id":42824,
                "name":"Carl Ciarfalio",
                "character":"Lou's Body Guard (as Carl N. Ciarfalio)",
                "order":30,
                "cast_id":48,
                "profile_path":null
            },
            {
                "id":40277,
                "name":"Stuart Blumberg",
                "character":"Car Salesman",
                "order":31,
                "cast_id":49,
                "profile_path":null
            },
            {
                "id":1270716,
                "name":"Todd Peirce",
                "character":"Man #1 at Auto Shop",
                "order":32,
                "cast_id":50,
                "profile_path":null
            },
            {
                "id":122805,
                "name":"Mark Fite",
                "character":"Man #2 at Auto Shop",
                "order":33,
                "cast_id":51,
                "profile_path":"/9On8A4VRctM8uuaJYE0KaLhDjcw.jpg"
            },
            {
                "id":35521,
                "name":"Matt Winston",
                "character":"Seminary Student",
                "order":34,
                "cast_id":52,
                "profile_path":"/6hGcDyUaewBHdwfmKYp6EP7otRh.jpg"
            },
            {
                "id":1270720,
                "name":"Joon Kim",
                "character":"Raymond K. Hessel (as Joon B. Kim)",
                "order":35,
                "cast_id":53,
                "profile_path":null
            },
            {
                "id":1010551,
                "name":"Bennie Moore",
                "character":"Bus Driver with Broken Nose (as Bennie E. Moore Jr.)",
                "order":36,
                "cast_id":54,
                "profile_path":null
            },
            {
                "id":1224996,
                "name":"Lauren Sánchez",
                "character":"Channel 4 Reporter (as W. Lauren Sanchez)",
                "order":37,
                "cast_id":55,
                "profile_path":null
            },
            {
                "id":1174793,
                "name":"Pat McNamara",
                "character":"Police Commissioner Jacobs",
                "order":38,
                "cast_id":56,
                "profile_path":null
            },
            {
                "id":1270723,
                "name":"Tyrone R. Livingston",
                "character":"Banquet Speaker",
                "order":39,
                "cast_id":57,
                "profile_path":null
            },
            {
                "id":1113343,
                "name":"Owen Masterson",
                "character":"Airport Valet",
                "order":40,
                "cast_id":58,
                "profile_path":null
            },
            {
                "id":109100,
                "name":"David Jean Thomas",
                "character":"Policeman (as David Jean-Thomas)",
                "order":41,
                "cast_id":59,
                "profile_path":null
            },
            {
                "id":160412,
                "name":"Paul Carafotes",
                "character":"Salvator, Winking Bartender",
                "order":42,
                "cast_id":60,
                "profile_path":"/yFRANT8p0D4j4PJclkd1psi5a0J.jpg"
            },
            {
                "id":145531,
                "name":"Christopher John Fields",
                "character":"Proprietor of Dry Cleaners",
                "order":43,
                "cast_id":61,
                "profile_path":"/jTWw4B74VhrPo8AN6Q9jq31eYDD.jpg"
            },
            {
                "id":182139,
                "name":"Anderson Bourell",
                "character":"Bruised Bar Patron #1",
                "order":44,
                "cast_id":62,
                "profile_path":null
            },
            {
                "id":63537,
                "name":"Scotch Ellis Loring",
                "character":"Bruised Bar Patron #2",
                "order":45,
                "cast_id":63,
                "profile_path":"/7Tk72GCd4TLfJj16EvVroEtMv86.jpg"
            },
            {
                "id":9291,
                "name":"Michael Shamus Wiles",
                "character":"Bartender in Halo",
                "order":46,
                "cast_id":64,
                "profile_path":"/upfSW6BGze446iqsZRehzcToNm8.jpg"
            },
            {
                "id":170315,"name":"Andi Carnick","character":"Hotel Desk Clerk","order":47,"cast_id":65,"profile_path":null},{"id":1270724,"name":"Edward Kowalczyk","character":"Waiter at Clifton's","order":48,"cast_id":66,"profile_path":null},{"id":7140,"name":"Leonard Termo","character":"Desk Sergeant","order":49,"cast_id":67,"profile_path":null},{"id":41352,"name":"Van Quattro","character":"Detective Andrew","order":50,"cast_id":68,"profile_path":null},{"id":140476,"name":"Markus Redmond","character":"Detective Kevin","order":51,"cast_id":69,"profile_path":null},{"id":177175,"name":"Michael Girardin","character":"Detective Walker","order":52,"cast_id":70,"profile_path":null},{"id":74507,"name":"Michael Arturo","character":"BMW Salesman (uncredited)","order":53,"cast_id":71,"profile_path":"/yMm3LVd8tm63B3e3DF415U5qRaI.jpg"},{"id":1194120,"name":"Matt Cinquanta","character":"Fighter (uncredited)","order":54,"cast_id":72,"profile_path":null},{"id":1270725,"name":"Tommy Dallace","character":"Champion Fighter (uncredited)","order":55,"cast_id":73,"profile_path":null},{"id":13925,"name":"Paul Dillon","character":"Irvin (uncredited)","order":56,"cast_id":74,"profile_path":"/6jouXtMcbJ7nTuDNwvaePnqCHTX.jpg"},{"id":552271,"name":"Eddie Hargitay","character":"Chanting Fighter (uncredited)","order":57,"cast_id":75,"profile_path":null},{"id":94561,"name":"Phil Hawn","character":"Banquet Guest (uncredited)","order":58,"cast_id":76,"profile_path":null},{"id":1270726,"name":"Jawara","character":"Fight Patron Saying 'What's going on?' (uncredited)","order":59,"cast_id":77,"profile_path":null},{"id":207609,"name":"Baron Jay","character":"Waiter (uncredited)","order":60,"cast_id":78,"profile_path":null},{"id":154636,"name":"Jim Jenkins","character":"Restaurant Maitre'd (uncredited)","order":61,"cast_id":79,"profile_path":null},{"id":1270727,"name":"Kevin Scott Mack","character":"Passenger Clutching Armrest (uncredited)","order":62,"cast_id":80,"profile_path":null},{"id":1270728,"name":"Trey Ore","character":"Fight Club Patron / Guy #2 in video store (uncredited)","order":63,"cast_id":81,"profile_path":null},{"id":1270729,"name":"Louis Ortiz","character":"Fight Spectator (uncredited)","order":64,"cast_id":82,"profile_path":null},{"id":1270730,"name":"Hugh Peddy","character":"Fight Club Man (uncredited)","order":65,"cast_id":83,"profile_path":null},{"id":1270731,"name":"J.T. Pontino","character":"Fight Club Man (uncredited)","order":66,"cast_id":84,"profile_path":null},{"id":1270732,"name":"Chad Randau","character":"Waiter (uncredited)","order":67,"cast_id":85,"profile_path":null},{"id":133153,"name":"Marcio Rosario","character":"Fighter (uncredited)","order":68,"cast_id":86,"profile_path":null},{"id":962235,"name":"Gregory Silva","character":"Riley Wilde, Fighter (uncredited)","order":69,"cast_id":87,"profile_path":null},{"id":16060,"name":"Brian Tochi","character":"Fight Bully (uncredited)","order":70,"cast_id":88,"profile_path":"/2nAC2ssCFu74zkhg1722WQhArGO.jpg"},{"id":1270733,"name":"Alekxia Valdez","character":"Bar Worker saying \"His name is Robert Paulsen\" (uncredited)","order":71,"cast_id":89,"profile_path":null
            }
        ],
    }
}

电影课:

public class Movie
{
    [JsonProperty(PropertyName = "id")]
    public int ID { get; set; }

    [JsonProperty(PropertyName = "original_title")]
    public string Title { get; set; }

    [JsonProperty(PropertyName = "cast"]
    public List Lists { get; set; }
}

Actor类:

public class Actor
{
    #region Properties
    [JsonProperty(PropertyName = "id")]
    public int ActorId { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }

    [JsonProperty(PropertyName = "character")]
    public string CharacterName { get; set; }

    [JsonProperty(PropertyName = "order")]
    public int Order { get; set; }

    [JsonProperty(PropertyName = "cast_id")]
    public int CastId { get; set; }

    [JsonProperty(PropertyName = "profile_path")]
    public string ProfilePath { get; set; }

    #endregion
}

我想访问该cast属性而不需要一个名为eg的类credits来存储actor的列表.我想直接存储类中的actor列表Movie.

反序列化的调用如下所示:

 Movie test = JsonConvert.DeserializeObject(json);

我试图JsonPropertyMovie课堂上编辑,cast.credits但没有任何作用.

我看到了类似问题的答案,但我不想手动这样做; 我想要它自动.Json获取底层数组而不检索其成员

1 个回答
  • 由于castJSON中的属性不在顶级对象(电影)中,并且您不想引入中间Credits类,因此您需要使用自定义JsonConverter来弥补差距.以下是转换器的外观:

    public class MovieConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return (objectType == typeof(Movie));
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject jo = JObject.Load(reader);
            Movie movie = jo.ToObject<Movie>();
            movie.Lists = jo.SelectToken("credits.cast").ToObject<List<Actor>>();
            return movie;
        }
    
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }
    

    要使用转换器,只需将它的实例传递给JsonConvert.DeserializeObject()方法,如下所示:

    Movie movie = JsonConvert.DeserializeObject<Movie>(json, new MovieConverter());
    

    这是一个演示:

    class Program
    {
        static void Main(string[] args)
        {
            string json = @"
            {
                ""id"": 550,
                ""original_title"": ""Fight Club"",
                ""credits"": {
                    ""cast"": [
                        {
                            ""id"": 819,
                            ""name"": ""Edward Norton"",
                            ""character"": ""The Narrator"",
                            ""order"": 0,
                            ""cast_id"": 4,
                            ""profile_path"": ""/eIkFHNlfretLS1spAcIoihKUS62.jpg""
                        },
                        {
                            ""id"": 287,
                            ""name"": ""Brad Pitt"",
                            ""character"": ""Tyler Durden"",
                            ""order"": 1,
                            ""cast_id"": 5,
                            ""profile_path"": ""/kc3M04QQAuZ9woUvH3Ju5T7ZqG5.jpg""
                        },
                        {
                            ""id"": 1283,
                            ""name"": ""Helena Bonham Carter"",
                            ""character"": ""Marla Singer"",
                            ""order"": 2,
                            ""cast_id"": 6,
                            ""profile_path"": ""/58oJPFG1wefMC0Vj7sFzHPrm67J.jpg""
                        },
                        {
                            ""id"": 7470,
                            ""name"": ""Meat Loaf"",
                            ""character"": ""Robert 'Bob' Paulson"",
                            ""order"": 3,
                            ""cast_id"": 7,
                            ""profile_path"": ""/pwNyXgegO1nlZ8uWT847JM8EjGj.jpg""
                        },
                        {
                            ""id"": 7471,
                            ""name"": ""Zach Grenier"",
                            ""character"": ""Richard Chesler"",
                            ""order"": 4,
                            ""cast_id"": 8,
                            ""profile_path"": ""/jghYiKdNkVehKpiVyE97AWrU9KQ.jpg""
                        }
                    ]
                }
            }";
    
            Movie movie = JsonConvert.DeserializeObject<Movie>(json, new MovieConverter());
            Console.WriteLine(movie.Title + " cast:");
            foreach (Actor actor in movie.Lists)
            {
                string sep = new string('\t', actor.Name.Length > 14 ? 1 : 2);
                Console.WriteLine(actor.Name + sep + actor.CharacterName);
            }
        }
    }
    

    输出:

    Fight Club cast:
    Edward Norton           The Narrator
    Brad Pitt               Tyler Durden
    Helena Bonham Carter    Marla Singer
    Meat Loaf               Robert 'Bob' Paulson
    Zach Grenier            Richard Chesler
    

    2023-02-13 19:05 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有