I want to parse data from IMDB.com to my c# application. I have a class named IMDB_Entry which looks like this:
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; }
And two classes named IMDB_Rating and IMDB_Person, the code for thoose is as follows:
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; }
}
In my Application I use HTML Agility pack to get a string containing all the nessecary data in JSON format, that looks like this:JSON
How can I get the data from the JSON string to my classes, I already tried it by going through the lines one by one, using if statements and replacing chars but it doesnt really work and it
s definitly not an elegant solution. I am very new to json, I googled a bit but can`t really work out what to do. Thanks in advance.
I would use newtonsoft.json to parse in the json dynamically into a dictionary. Then accessing the data is easy. Examples here. https://www.newtonsoft.com/json/help/html/Introduction.htm
You could also use JavaScript serializer to parse the json. Example here https://www.codementor.io/andrewbuchan/how-to-parse-json-into-a-c-object-4ui1o0bx8
Hope that helps. If your still stuck, let me know and I would be glad to help further. You can also reach out to me directly for help on Twitter. https://twitter.com/NickCGamb