【发布时间】:2021-10-03 09:41:48
【问题描述】:
首先,当然我已经搜索了大约两天,但如果可能的话,我无法让它工作。
这有点帮助: Get property value from C# dynamic object by string (reflection?) 还有这个: C# Iterate through Class properties
背景资料: 该类是使用来自 TMDB 的 JSON 字符串生成的。 API 密钥是免费的,因此如果您想查看原始 json,请获取密钥并使用它: https://api.themoviedb.org/3/tv/456?api_key=YOURKEY&append_to_response=season/1,season/2,season/3&language=en-US 我有 20 个赛季,但在这里缩短了。一次只允许 20 个。是的,我可以一次获得一个赛季并进行 27 次 api 调用,但 2 次更好。获得一季效果很好,不需要任何帮助。
这是使用VS2015特殊粘贴生成的json类的一部分,并做了一些调整:
namespace TvDb.Tv
{
class MultiSeasons
{
public Season[] seasons { get; set; }
public Season1 season1 { get; set; }
public Season2 season2 { get; set; }
public Season3 season3 { get; set; }
//and so on
}
public class Season1 : CommonProperties { }
public class Season2 : CommonProperties { }
public class Season3 : CommonProperties { }
//and so on
public class CommonProperties
{
public string _id { get; set; }
public string air_date { get; set; }
public Episode[] episodes { get; set; }
public string name { get; set; }
public string overview { get; set; }
public string poster_path { get; set; }
public int season_number { get; set; }
}
public class Season
{
public string air_date { get; set; }
public int episode_count { get; set; }
public int id { get; set; }
public string name { get; set; }
public string overview { get; set; }
public string poster_path { get; set; }
public int season_number { get; set; }
}
public class Episode
{
public string air_date { get; set; }
public int episode_number { get; set; }
public int id { get; set; }
public string name { get; set; }
public string overview { get; set; }
public string production_code { get; set; }
public int season_number { get; set; }
public string still_path { get; set; }
public float vote_average { get; set; }
public int vote_count { get; set; }
}
}
我可以通过反序列化 json 获得所需的信息,但可以看出,我知道动态获取类可以更好地编写。我只是似乎找不到方法....
string json = "It's stored in a file for testing";
//the json from TMDB returns wrong to name or find classes
//such as season/1, season/2, etc.
json = json.Replace("season/", "season");
MultiSeasons ser = JsonConvert.DeserializeObject<MultiSeasons>(json);
DataTable dt = new DataTable();
//columns added here
//int numOfSeasons = ser.number_of_seasons;
//Type type = Type.GetType("TvDb.Tv.Series, TvDb");
string str = "TvDb.Tv.Season1" + ", " + "TvDb";
//dynamic d =
Type type = Type.GetType(str);
string st = "";
if (type != null)
{
var cls = Utilities.Classes.PropertyReader.getProperties(type);
//PropertyReader site https://answers.unity.com/questions/1092425/how-to-get-a-list-of-variables-in-a-class.html
for (int i = 0; i < cls.Length; i++)
{
if(cls[i].name == "episodes")
{
//Episode e =
}
var nameOfProperty = cls[i].name;
var propertyInfo = cls.GetType().GetProperty(nameOfProperty);
var value = propertyInfo.GetValue(cls, null);
//st += cls.GetType().GetProperty(cls[i].name).GetValue(cls, null);
//st += cls[i].name.ToString() + Environment.NewLine;
}
}
DataRow dr;
//write 50 seperate foreach statements? I don't think so !!!!!!!!
if (ser.season1 != null)
{
foreach (var r in ser.season1.episodes)
{
dr = dt.NewRow();
dr["id"] = ser.id;
dr["firstAired"] = r.air_date;
dr["season"] = r.season_number;
//and so on
dt.Rows.Add(dr);
}
}
if (ser.season2 != null)
{
foreach (var r in ser.season2.episodes)
{
dr = dt.NewRow();
dr["id"] = ser.id;
dr["firstAired"] = r.air_date;
dr["season"] = r.season_number;
//and so on
dt.Rows.Add(dr);
}
}
感谢任何输入或将我指向我错过的帖子。
编辑:对于那些想看json的人,我找到了一个可以查看的地方......
【问题讨论】:
-
为什么
Season1、Season2和Season3是不同的类型? -
我猜 Season1、Season2 (等)是不同的类型,因为这是 JsonConvert.DeserializeObject 在这种情况下所期望的结构。无论如何,如果可能的话,我会尽量消除这种需求。也许使用你的“json.Replace”行来一起去掉季节编号(所以'season/1'变成了'season'),然后实际的季节编号存在于Season类的'season_number'属性中。
-
@Leetheus 这是个好主意。我确实尝试过,但即使在季节(Episode [])中添加一个属性,它也是空的。我什至复制了调整后的json,并在VS中做了一个特殊的粘贴,果然有第1季,第2季等。
-
@john Wu
Season[] seasons属性只是季节信息,而不是剧集。 -
给我几分钟 :)