我从iTunes返回了以下Json(我已经替换了一些隐私细节,例如用户名和评论内容(
{"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 ...
我想将其反序列化为一个类。我只需要评论标题,评论内容和"im:rating"(即星数(。但是,由于嵌套的 Json 以及如何提取这些数据的键的使用,我正在苦苦挣扎。到目前为止,我已经创建了一个准备反序列化的类,并且我AppStoreData appStoreData = JsonConvert.DeserializeObject<AppStoreData>(stringResult);
反序列化它
public class AppStoreData {
}
我的问题是我不知道在类中放什么才能从 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 是一个类
public class Id
{
[JsonProperty("label")]
public string Label { get; set; }
}
或者编写一个反序列化程序,为您将其转换为字符串。
我建议尝试许多免费代码生成器之一,如 https://app.quicktype.io/?l=csharp 或 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);
}