C#JSON到类[重复]

问题描述 投票:-2回答:1

这个问题在这里已有答案:

我想将数据从IMDB.com解析到我的c#应用程序。我有一个名为IMDB_Entry的类,如下所示:

private string type;
    private string url;
    private string name;
    private string image;
    private List<String> genre;
    private string content_rating;
    private List<IMDB_Entry> actors;
    private List<IMDB_Person> directors;
    private List<IMDB_Person> creators;
    private string description;
    private string date;
    private List<String> keywords;
    private IMDB_Rating rating;
    private string duration;

    public string Type { get => type; set => type = value; }
    public string Url { get => url; set => url = value; }
    public string Name { get => name; set => name = value; }
    public string Image { get => image; set => image = value; }
    public List<string> Genre { get => genre; set => genre = value; }
    public string Content_rating { get => content_rating; set => content_rating = value; }
    public  List<IMDB_Entry> Actors { get => actors; set => actors = value; }
    public List<IMDB_Person> Directors { get => directors; set => directors = value; }
    public List<IMDB_Person> Creators { get => creators; set => creators = value; }
    public string Description { get => description; set => description = value; }
    public string Date { get => Date1; set => Date1 = value; }
    public string Date1 { get => date; set => date = value; }
    public List<string> Keywords { get => keywords; set => keywords = value; }
    public IMDB_Rating Rating { get => rating; set => rating = value; }
    public string Duration { get => duration; set => duration = value; }

还有两个名为IMDB Rating和IMDB_Person的类,其代码如下:

public class IMDB_Person
{
    private string url;
    private string name;

    public string Name { get => name; set => name = value; }
    public string Url { get => url; set => url = value; }
}
public class IMDB_Rating
{
    public long Rating_count { get; set; }
    public decimal Best_Rating { get; set; }
    public decimal Worst_Rating { get; set; }
    public decimal Average_Rating { get; set; }
}

在我的应用程序中,我使用HTML Agility包来获取包含JSON格式的所有nessecary数据的字符串,如下所示:JSON

我如何从JSON字符串中获取数据到我的类,我已经通过逐行遍历,使用if语句和替换字符来尝试它,但它确实不是一个优雅的解决方案nt really work and its。我是json的新手,我用google搜索了一下但是真的不知道该做什么。提前致谢。

c# json parsing web-scraping imdb
1个回答
1
投票

我会使用newtonsoft.json动态地将json解析为字典。然后访问数据很容易。这里的例子。 https://www.newtonsoft.com/json/help/html/Introduction.htm

您还可以使用JavaScript序列化程序来解析json。这里的例子是https://www.codementor.io/andrewbuchan/how-to-parse-json-into-a-c-object-4ui1o0bx8

希望有所帮助。如果你仍然卡住了,请告诉我,我很乐意进一步提供帮助。您也可以直接联系我以获取Twitter上的帮助。 https://twitter.com/NickCGamb

© www.soinside.com 2019 - 2024. All rights reserved.