【问题标题】:Deserializing Json from iTunes store into C# object将 iTunes 商店中的 Json 反序列化为 C# 对象
【发布时间】: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


【解决方案1】:

要解决 im:rating 的问题,您可以使用 JsonProperty 属性

    [JsonProperty("im:rating")]
    public Id ImRating { get; set; }

将诸如标签之类的东西转换为字符串属性的问题在于它不是字符串,而是输入文件中的一个对象。 你需要类似的东西

     [JsonProperty("title")]
     public Id Title { get; set; }

其中 Id 是一个类

public class Id
{
    [JsonProperty("label")]
    public string Label { get; set; }
}

或者编写一个反序列化器,为您将其转换为字符串。

我建议尝试使用许多免费的代码生成器之一,例如 https://app.quicktype.io/?l=csharphttp://json2csharp.com/,以抢占先机,然后根据自己的喜好编辑所有内容。

【讨论】:

【解决方案2】:

我建议使用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);
}

【讨论】:

    猜你喜欢
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    相关资源
    最近更新 更多