简体   繁体   English

如果json对象的名称以int开头,如何将json反序列化为c#模型

[英]How do I deserialize json to c# model if the json object has a name starting with an int

I'm trying to work on updating a JIRA API plugin because I need to do more with it than it's current limitations. 我正在尝试更新JIRA API插件,因为除了当前的局限性之外,我还需要做更多的事情。 The problem is Jira has a field of avatarUrls that have names like 48x48 32x32 I am using deserializer.Deserialize<CommentsContainer>(response) in order to deserialize the json to an object. 问题是Jira有一个avatarUrl字段,其名称类似于48x48 32x32我正在使用deserializer.Deserialize deserializer.Deserialize<CommentsContainer>(response)以将json反序列化为对象。 Any ideas how I might capture this information? 有什么想法可以捕获这些信息吗? I need these avatars displayed on my page. 我需要在页面上显示这些头像。 Thanks for your help. 谢谢你的帮助。

I recomender to use JSON.NET it is very useful librery for working with tricky Json like on Jira API. 我建议使用JSON.NET,这对于在Jira API上处理棘手的Json是非常有用的自由。 I have used this approach for your perpouse 我用这种方法为您辩护

Entity: 实体:

public class ProjectDescription : BaseEntity
{
    public int Id { get; set; }    
    public string Key { get; set; }    
    public string Name { get; set; }

    [JsonProperty("avatarUrls")]
    public AvatarUrls AvatarUrls { get; set; } 
}  

public class AvatarUrls
{
  [JsonProperty("32x32")]
  public string Size32 { get; set; }

  [JsonProperty("48x48")]
  public string Size48 { get; set; }
}

And just Deserialize your responce to Enity: 然后反序列化对Enity的响应:

var projects = JsonConvert.DeserializeObject<List<ProjectDescription>>(response);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM