如何在c#中使用数组迭代作为反序列化json RootObject的标识符?

时间:2018-01-21 10:54:50

标签: c# json

public class Rating
{
    public string Source { get; set; }
    public string Value { get; set; }
}

public class RootObject
{
    public string Title { get; set; }
    public string Year { get; set; }
    public string Rated { get; set; }
    public string Released { get; set; }
    public string Runtime { get; set; }
    public string Genre { get; set; }
    public string Director { get; set; }
    public string Writer { get; set; }
    public string Actors { get; set; }
    public string Plot { get; set; }
    public string Language { get; set; }
    public string Country { get; set; }
    public string Awards { get; set; }
    public string Poster { get; set; }
    public List<Rating> Ratings { get; set; }
    public string Metascore { get; set; }
    public string imdbRating { get; set; }
    public string imdbVotes { get; set; }
    public string imdbID { get; set; }
    public string Type { get; set; }
    public string DVD { get; set; }
    public string BoxOffice { get; set; }
    public string Production { get; set; }
    public string Website { get; set; }
    public string Response { get; set; }
}
// JsonObject this is the c# class.cs file
public static string getJson()
        {
            string titleOfDesiredMovie = Console.ReadLine();
            WebClient client = new WebClient();
            string json = client.DownloadString("http://www.omdbapi.com/?t=" + titleOfDesiredMovie + "&apikey=SECRETKEY");
            return json;
        }
        static void Main(string[] args)
        {
            RootObject movie = new JavaScriptSerializer().Deserialize<RootObject>(getJson());
            string[] array = {
                                 "Title",
                                 "Year",
                                 "Rated",
                                 "Genre",
                                 "Writer",
                                 "Plot"
                             };
            for (int i = 0; i < array.Length;i++ )
            { 
                foreach (var item in movie.array[i])
                {
                    Console.Write(item);
                }
            Console.WriteLine();
            }
            var key = Console.ReadKey();
        }
// the main file

我正在使用C#,我正在开发一个控制台应用程序,用于使用OMDB API获取某些电影的信息。 当我输入array [i]作为电影类的标识符时,我遇到了问题。 PS:这是我的第一个问题,如果您对改进有任何反馈,请告诉我。

1 个答案:

答案 0 :(得分:1)

显然,您希望输出某些返回属性的值。因为您将JSON反序列化为对象,所以您必须使用反射,或者使用dynamic

更好的解决方案是将JSON解析为JObject,然后使用此对象的索引:

JObject movie = JObject.Parse(getJson());
string[] array = {
                        "Title",
                        "Year",
                        "Rated",
                        "Genre",
                        "Writer",
                        "Plot"
                    };
for (int i = 0; i < array.Length; i++)
{
    Console.Write(movie[array[i]].ToString());
}
var key = Console.ReadKey();

我正在使用NuGet的Newtonsoft.Json库。

如果您想使用反射,则可以执行以下操作:

RootObject movie = new JavaScriptSerializer().Deserialize<RootObject>(getJson());
string[] array = {
                        "Title",
                        "Year",
                        "Rated",
                        "Genre",
                        "Writer",
                        "Plot"
                    };
for (int i = 0; i < array.Length; i++)
{
    var propertyInfo = movie.GetType().GetProperty(array[i]);
    Console.WriteLine(propertyInfo.GetValue(movie).ToString());
    Console.WriteLine();
}
var key = Console.ReadKey();