C# JSON 到 class
C# JSON to class
我想将数据从 IMDB.com 解析到我的 C# 应用程序。我有一个名为 IMDB_Entry 的 class,它看起来像这样:
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; }
和两个class分别命名为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 敏捷包来获取包含 JSON 格式的所有必需数据的字符串,如下所示:JSON
如何从 JSON 字符串获取数据到我的 classes,我已经通过逐行逐行尝试过,使用 if 语句并替换字符,但它没有t really work and it
这绝对不是一个优雅的解决方案。我是 json 的新手,我用谷歌搜索了一下,但真的不知道该怎么做。提前致谢。
我会使用 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
我想将数据从 IMDB.com 解析到我的 C# 应用程序。我有一个名为 IMDB_Entry 的 class,它看起来像这样:
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; }
和两个class分别命名为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 敏捷包来获取包含 JSON 格式的所有必需数据的字符串,如下所示:JSON
如何从 JSON 字符串获取数据到我的 classes,我已经通过逐行逐行尝试过,使用 if 语句并替换字符,但它没有t really work and it
这绝对不是一个优雅的解决方案。我是 json 的新手,我用谷歌搜索了一下,但真的不知道该怎么做。提前致谢。
我会使用 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