【发布时间】:2019-11-21 15:23:10
【问题描述】:
我有以下从 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# 中是一个无效名称。
有人可以帮忙吗?谢谢
【问题讨论】:
-
Visual Studio 中有一项功能允许您将 JSON 粘贴为类结构。 1. 将完整的 json 复制到剪贴板 2. 打开 Visual Studio 类并单击编辑 > 选择性粘贴 > 将 JSON 粘贴为类 3. 删除不需要的额外属性。
-
只需从您的 JSON 中创建您的课程here。
标签: c# .net json api app-store