Taylor Mitchell
Taylor Mitchell

Reputation: 613

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. 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. Any ideas how I might capture this information? I need these avatars displayed on my page. Thanks for your help.

Upvotes: 1

Views: 193

Answers (1)

Ilya Sulimanov
Ilya Sulimanov

Reputation: 7846

I recomender to use JSON.NET it is very useful librery for working with tricky Json like on Jira API. 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:

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

Upvotes: 2

Related Questions