将 Json 从 iTunes 商店反序列化为 C# object
Deserializing Json from iTunes store into C# object
我有以下 Json 从 iTunes 返回(我已经替换了一些隐私细节,例如用户名和评论内容)
{"feed":{"author":{"name":{"label":"iTunes Store"}, "uri":{"label":"http://www.apple.com/uk/itunes/"}}, "entry":[
{"author":{"uri":{"label":"https://itunes.apple.com/gb/reviews/ID"}, "name":{"label":"Username"}, "label":""}, "im:version":{"label":"3.51"}, "im:rating":{"label":"4"}, "id":{"label":"12345"}, "title":{"label":"Review title"}, "content":{"label":"Review contents", "attributes":{"type":"text"}}, "link":{"attributes":{"rel":"related", "href":"https://itunes.apple.com/gb/review?reviewid"}}, "im:voteSum":{"label":"0"}, "im:contentType":{"attributes":{"term":"Application", "label":"Application"}}, "im:voteCount":{"label":"0"}},
// more entries ...
我想将其反序列化为 class。我只需要评论标题、评论内容和 "im:rating"(即星数)。但是,由于嵌套的 Json 和如何提取此数据的键的使用,我正在苦苦挣扎。到目前为止,我已经创建了一个 class 准备反序列化,我有 AppStoreData appStoreData = JsonConvert.DeserializeObject<AppStoreData>(stringResult);
来反序列化它
public class AppStoreData {
}
我的问题是我不知道要在 class 中放入什么才能从 Json 中获取我需要的信息。我试过这样的事情:
public string title {get; set;}
public string content {get; set;}
public string imrating {get; set;}
但是这不起作用。我还认为 im:rating
在 C# 中是一个无效名称。
有人可以帮忙吗?谢谢
要解决 im:rating 的问题,您可以使用 JsonProperty 属性
[JsonProperty("im:rating")]
public Id ImRating { get; set; }
将 Label 之类的东西转换为字符串的问题 属性 是它不是字符串,而是输入文件中的对象。
你需要像
这样的东西
[JsonProperty("title")]
public Id Title { get; set; }
其中 Id 是 class
public class Id
{
[JsonProperty("label")]
public string Label { get; set; }
}
或者编写一个反序列化程序,为您将其转换为字符串。
我建议尝试使用 https://app.quicktype.io/?l=csharp or http://json2csharp.com/ 等众多免费代码生成器中的一种来抢先一步,然后根据自己的喜好编辑所有内容。
我建议使用 json2csharp 将 json 转换为 c# 模型并通过添加 JsonProperty
更改无效属性
public class Name
{
public string label { get; set; }
}
public class Uri
{
public string label { get; set; }
}
public class Author
{
public Name name { get; set; }
public Uri uri { get; set; }
}
public class Uri2
{
public string label { get; set; }
}
public class Name2
{
public string label { get; set; }
}
public class Author2
{
public Uri2 uri { get; set; }
public Name2 name { get; set; }
public string label { get; set; }
}
public class ImVersion
{
public string label { get; set; }
}
public class ImRating
{
public string label { get; set; }
}
public class Id
{
public string label { get; set; }
}
public class Title
{
public string label { get; set; }
}
public class Attributes
{
public string type { get; set; }
}
public class Content
{
public string label { get; set; }
public Attributes attributes { get; set; }
}
public class Attributes2
{
public string rel { get; set; }
public string href { get; set; }
}
public class Link
{
public Attributes2 attributes { get; set; }
}
public class ImVoteSum
{
public string label { get; set; }
}
public class Attributes3
{
public string term { get; set; }
public string label { get; set; }
}
public class ImContentType
{
public Attributes3 attributes { get; set; }
}
public class ImVoteCount
{
public string label { get; set; }
}
public class Entry
{
public Author2 author { get; set; }
[JsonProperty(PropertyName = "im:version")]
public ImVersion imversion { get; set; }
[JsonProperty(PropertyName = "im:rating")]
public ImRating imrating { get; set; }
public Id id { get; set; }
public Title title { get; set; }
public Content content { get; set; }
public Link link { get; set; }
[JsonProperty(PropertyName = "im:voteSum")]
public ImVoteSum imvoteSum { get; set; }
[JsonProperty(PropertyName = "im:contentType")]
public ImContentType imcontentType { get; set; }
[JsonProperty(PropertyName = "im:voteCount")]
public ImVoteCount imvoteCount { get; set; }
}
public class Feed
{
public Author author { get; set; }
public List<Entry> entry { get; set; }
}
public class RootObject5
{
public Feed feed { get; set; }
}
模型准备就绪后,您可以使用 JsonConvert.DeserializeObject
将 JSON 转换为 C# 对象
using (StreamReader r = new StreamReader(filepath)) {
string json = r.ReadToEnd();
var newEmps = JsonConvert.DeserializeObject<RootObject5>(json);
Console.WriteLine(newEmps.feed.entry[0].imvoteCount.label);
}
我有以下 Json 从 iTunes 返回(我已经替换了一些隐私细节,例如用户名和评论内容)
{"feed":{"author":{"name":{"label":"iTunes Store"}, "uri":{"label":"http://www.apple.com/uk/itunes/"}}, "entry":[
{"author":{"uri":{"label":"https://itunes.apple.com/gb/reviews/ID"}, "name":{"label":"Username"}, "label":""}, "im:version":{"label":"3.51"}, "im:rating":{"label":"4"}, "id":{"label":"12345"}, "title":{"label":"Review title"}, "content":{"label":"Review contents", "attributes":{"type":"text"}}, "link":{"attributes":{"rel":"related", "href":"https://itunes.apple.com/gb/review?reviewid"}}, "im:voteSum":{"label":"0"}, "im:contentType":{"attributes":{"term":"Application", "label":"Application"}}, "im:voteCount":{"label":"0"}},
// more entries ...
我想将其反序列化为 class。我只需要评论标题、评论内容和 "im:rating"(即星数)。但是,由于嵌套的 Json 和如何提取此数据的键的使用,我正在苦苦挣扎。到目前为止,我已经创建了一个 class 准备反序列化,我有 AppStoreData appStoreData = JsonConvert.DeserializeObject<AppStoreData>(stringResult);
来反序列化它
public class AppStoreData {
}
我的问题是我不知道要在 class 中放入什么才能从 Json 中获取我需要的信息。我试过这样的事情:
public string title {get; set;}
public string content {get; set;}
public string imrating {get; set;}
但是这不起作用。我还认为 im:rating
在 C# 中是一个无效名称。
有人可以帮忙吗?谢谢
要解决 im:rating 的问题,您可以使用 JsonProperty 属性
[JsonProperty("im:rating")]
public Id ImRating { get; set; }
将 Label 之类的东西转换为字符串的问题 属性 是它不是字符串,而是输入文件中的对象。 你需要像
这样的东西 [JsonProperty("title")]
public Id Title { get; set; }
其中 Id 是 class
public class Id
{
[JsonProperty("label")]
public string Label { get; set; }
}
或者编写一个反序列化程序,为您将其转换为字符串。
我建议尝试使用 https://app.quicktype.io/?l=csharp or http://json2csharp.com/ 等众多免费代码生成器中的一种来抢先一步,然后根据自己的喜好编辑所有内容。
我建议使用 json2csharp 将 json 转换为 c# 模型并通过添加 JsonProperty
public class Name
{
public string label { get; set; }
}
public class Uri
{
public string label { get; set; }
}
public class Author
{
public Name name { get; set; }
public Uri uri { get; set; }
}
public class Uri2
{
public string label { get; set; }
}
public class Name2
{
public string label { get; set; }
}
public class Author2
{
public Uri2 uri { get; set; }
public Name2 name { get; set; }
public string label { get; set; }
}
public class ImVersion
{
public string label { get; set; }
}
public class ImRating
{
public string label { get; set; }
}
public class Id
{
public string label { get; set; }
}
public class Title
{
public string label { get; set; }
}
public class Attributes
{
public string type { get; set; }
}
public class Content
{
public string label { get; set; }
public Attributes attributes { get; set; }
}
public class Attributes2
{
public string rel { get; set; }
public string href { get; set; }
}
public class Link
{
public Attributes2 attributes { get; set; }
}
public class ImVoteSum
{
public string label { get; set; }
}
public class Attributes3
{
public string term { get; set; }
public string label { get; set; }
}
public class ImContentType
{
public Attributes3 attributes { get; set; }
}
public class ImVoteCount
{
public string label { get; set; }
}
public class Entry
{
public Author2 author { get; set; }
[JsonProperty(PropertyName = "im:version")]
public ImVersion imversion { get; set; }
[JsonProperty(PropertyName = "im:rating")]
public ImRating imrating { get; set; }
public Id id { get; set; }
public Title title { get; set; }
public Content content { get; set; }
public Link link { get; set; }
[JsonProperty(PropertyName = "im:voteSum")]
public ImVoteSum imvoteSum { get; set; }
[JsonProperty(PropertyName = "im:contentType")]
public ImContentType imcontentType { get; set; }
[JsonProperty(PropertyName = "im:voteCount")]
public ImVoteCount imvoteCount { get; set; }
}
public class Feed
{
public Author author { get; set; }
public List<Entry> entry { get; set; }
}
public class RootObject5
{
public Feed feed { get; set; }
}
模型准备就绪后,您可以使用 JsonConvert.DeserializeObject
将 JSON 转换为 C# 对象
using (StreamReader r = new StreamReader(filepath)) {
string json = r.ReadToEnd();
var newEmps = JsonConvert.DeserializeObject<RootObject5>(json);
Console.WriteLine(newEmps.feed.entry[0].imvoteCount.label);
}