我从themoviedb.org获得了一些json代码
{
"adult": false,
"backdrop_path": "/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg",
"belongs_to_collection": null,
"budget": 63000000,
"genres": [
{
"id": 28,
"name": "Action"
},
{
"id": 18,
"name": "Drama"
},
{
"id": 53,
"name": "Thriller"
}
],
"homepage": "",
"id": 550,
"imdb_id": "tt0137523",
"original_title": "Fight Club",
"overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.",
"popularity": 61151.745000000003,
"poster_path": "/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg",
"production_companies": [
{
"name": "20th Century Fox",
"id": 25
}
],
"production_countries": [
{
"iso_3166_1": "DE",
"name": "Germany"
},
{
"iso_3166_1": "US",
"name": "United States of America"
}
],
"release_date": "1999-10-15",
"revenue": 100853753,
"runtime": 139,
"spoken_languages": [
{
"iso_639_1": "en",
"name": "English"
}
],
"status": "Released",
"tagline": "How much can you know about yourself if you've never been in a fight?",
"title": "Fight Club",
"vote_average": 9.0999999999999996,
"vote_count": 174
"credits":{
"cast": [
{
"id": 819,
"name": "Edward Norton",
"character": "The Narrator",
"order": 0,
"cast_id": 4,
"profile_path": "/588Hrov6wwM9WcU88nJHlw2iufN.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": 7,
"cast_id": 11,
"profile_path": null
},
{
"id": 7497,
"name": "Holt McCallany",
"character": "The Mechanic",
"order": 8,
"cast_id": 22,
"profile_path": "/hQBfcw9KVszdenlTZTR8AIrSpex.jpg"
},
{
"id": 7498,
"name": "Eion Bailey",
"character": "Ricky",
"order": 9,
"cast_id": 23,
"profile_path": "/4MnRgrwuiJvHsfoiJrIUL4TkfoC.jpg"
},
{
"id": 7499,
"name": "Jared Leto",
"character": "Angel Face",
"order": 10,
"cast_id": 24,
"profile_path": "/lzQSuu5o0JC9mCncYjVnncSH5lO.jpg"
},
{
"id": 7500,
"name": "Peter Iacangelo",
"character": "Lou",
"order": 11,
"cast_id": 25,
"profile_path": null
},
{
"id": 56112,
"name": "David Lee Smith",
"character": "Walter",
"order": 12,
"cast_id": 30,
"profile_path": "/xYkMA9AWtUN93KV5hWzlDkcnebB.jpg"
}
],
"crew": [
{
"id": 7469,
"name": "Jim Uhls",
"department": "Writing",
"job": "Author",
"profile_path": null
},
{
"id": 7474,
"name": "Ross Grayson Bell",
"department": "Production",
"job": "Producer",
"profile_path": null
},
{
"id": 7475,
"name": "Ceán Chaffin",
"department": "Production",
"job": "Producer",
"profile_path": null
},
{
"id": 1254,
"name": "Art Linson",
"department": "Production",
"job": "Producer",
"profile_path": "/dEtVivCXxQBtIzmJcUNupT1AB4H.jpg"
},
{
"id": 7477,
"name": "John King",
"department": "Sound",
"job": "Original Music Composer",
"profile_path": null
},
{
"id": 7478,
"name": "Michael Simpson",
"department": "Sound",
"job": "Original Music Composer",
"profile_path": null
},
{
"id": 7479,
"name": "Jeff Cronenweth",
"department": "Camera",
"job": "Director of Photography",
"profile_path": null
},
{
"id": 7480,
"name": "James Haygood",
"department": "Editing",
"job": "Editor",
"profile_path": null
},
{
"id": 7481,
"name": "Laray Mayfield",
"department": "Production",
"job": "Casting",
"profile_path": null
},
{
"id": 1303,
"name": "Alex McDowell",
"department": "Art",
"job": "Production Design",
"profile_path": null
},
{
"id": 7763,
"name": "Ren Klyce",
"department": "Sound",
"job": "Sound Editor",
"profile_path": null
},
{
"id": 7764,
"name": "Richard Hymns",
"department": "Sound",
"job": "Sound Editor",
"profile_path": null
},
{
"id": 7467,
"name": "David Fincher",
"department": "Directing",
"job": "Director",
"profile_path": "/dcBHejOsKvzVZVozWJAPzYthb8X.jpg"
},
{
"id": 7468,
"name": "Chuck Palahniuk",
"department": "Writing",
"job": "Novel",
"profile_path": "/8nOJDJ6SqwV2h7PjdLBDTvIxXvx.jpg"
}]
}
}
我已经有了一个可以存储关于强制转换的信息的类,所以不要只是为了存储“Credits”属性来创建一个类,就像我现在正在做的那样。我希望将基础成员强制转换并反序列化为我的演员列表。
[JsonProperty(PropertyName = "credits")]
public Credits ActorsInMedia { get; set; }
public class Credits
{
[JsonProperty(PropertyName = "cast")]
public List<Actor> List { get; set; }
}
我不希望上面有一个全新的类,我想要这个而不需要创建一个新类。 [JsonProperty(PropertyName =“cast”)] 公共列表清单{get;组; }
我已经尝试过[JsonProperty(PropertyName =“credits.cast”)]和[JsonProperty(PropertyName =“credits {cast}”)]
什么都行不通,我怎样才能获得基础演员项目并检索..
这就是我解释的方式
Movie test = JsonConvert.DeserializeObject<Movie>(json);
答案 0 :(得分:0)
以下代码应该可以解决问题。 有关详情,请点击Here
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace JsonExp
{
class Program
{
static void Main(string[] args)
{
string json = File.ReadAllText(@"D:\json\jsonResponse.txt");
JObject jsonResults = JObject.Parse(json);
// get JSON result objects into a list
IList<JToken> results = jsonResults ["credits"]["cast"].Children().ToList();
// serialize JSON results into .NET objects
IList<Actor> searchResults = new List<Actor>();
foreach (JToken result in results)
{
Actor searchResult = JsonConvert.DeserializeObject<Actor>(result.ToString());
searchResults.Add(searchResult);
}
foreach (var actor in searchResults)
{
Console.WriteLine("{1}[{0}] as {2} Path: {3}", actor.Name, actor.Id, actor.Character, actor.ProfilePath);
}
Console.ReadKey();
}
}
#region Json classes
public class Credits
{
[JsonProperty(PropertyName = "cast")]
public List<Actor> List { get; set; }
}
public class Actor
{
[JsonProperty(PropertyName = "id")]
public int Id { get; set; }
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "character")]
public string Character { get; set; }
[JsonProperty(PropertyName = "order")]
public string Order { get; set; }
[JsonProperty(PropertyName = "cast_id")]
public string CastId { get; set; }
[JsonProperty(PropertyName = "profile_path")]
public string ProfilePath { get; set; }
}
#endregion
}
答案 1 :(得分:0)
您可以使用
等手动检索内容dynamic data = Newtonsoft.Json.Linq.JObject.Parse(content);
var value = data.credits.cast as Newtonsoft.Json.Linq.JArray;
var actors = value.Select(x => x.ToObject<Actor>());