Skip to content

Instantly share code, notes, and snippets.

@mchinchilla
Created April 7, 2024 05:37
Show Gist options
  • Save mchinchilla/4d19e3cbf4ac901b65bfa1f11063571a to your computer and use it in GitHub Desktop.
Save mchinchilla/4d19e3cbf4ac901b65bfa1f11063571a to your computer and use it in GitHub Desktop.
using System.Text.Json;
using System.Text.Json.Serialization;
string json = """
{
"2024-03-30": {
"comments": {
"total": 0
},
"follows": {
"total": 1
},
"reactions": {
"total": 0,
"like": 0,
"readinglist": 0,
"unicorn": 0
},
"page_views": {
"total": 48,
"average_read_time_in_seconds": 231,
"total_read_time_in_seconds": 11088
}
},
"2024-03-31": {
"comments": {
"total": 0
},
"follows": {
"total": 0
},
"reactions": {
"total": 0,
"like": 0,
"readinglist": 0,
"unicorn": 0
},
"page_views": {
"total": 99,
"average_read_time_in_seconds": 45,
"total_read_time_in_seconds": 4455
}
},
"2024-04-01": {
"comments": {
"total": 0
},
"follows": {
"total": 0
},
"reactions": {
"total": 0,
"like": 0,
"readinglist": 0,
"unicorn": 0
},
"page_views": {
"total": 227,
"average_read_time_in_seconds": 212,
"total_read_time_in_seconds": 48124
}
},
"2024-04-02": {
"comments": {
"total": 0
},
"follows": {
"total": 2
},
"reactions": {
"total": 0,
"like": 0,
"readinglist": 0,
"unicorn": 0
},
"page_views": {
"total": 348,
"average_read_time_in_seconds": 188,
"total_read_time_in_seconds": 65424
}
},
"2024-04-03": {
"comments": {
"total": 0
},
"follows": {
"total": 2
},
"reactions": {
"total": 0,
"like": 0,
"readinglist": 0,
"unicorn": 0
},
"page_views": {
"total": 303,
"average_read_time_in_seconds": 620,
"total_read_time_in_seconds": 187860
}
},
"2024-04-04": {
"comments": {
"total": 0
},
"follows": {
"total": 0
},
"reactions": {
"total": 0,
"like": 0,
"readinglist": 0,
"unicorn": 0
},
"page_views": {
"total": 305,
"average_read_time_in_seconds": 481,
"total_read_time_in_seconds": 146705
}
},
"2024-04-05": {
"comments": {
"total": 1
},
"follows": {
"total": 1
},
"reactions": {
"total": 0,
"like": 0,
"readinglist": 0,
"unicorn": 0
},
"page_views": {
"total": 298,
"average_read_time_in_seconds": 302,
"total_read_time_in_seconds": 89996
}
},
"2024-04-06": {
"comments": {
"total": 0
},
"follows": {
"total": 0
},
"reactions": {
"total": 0,
"like": 0,
"readinglist": 0,
"unicorn": 0
},
"page_views": {
"total": 52,
"average_read_time_in_seconds": 23,
"total_read_time_in_seconds": 1196
}
}
}
""";
Dictionary<DateTime, Stats>? statsDict = JsonSerializer.Deserialize<Dictionary<DateTime, Stats>>( json );
List<StatsByDate> statsList = new List<StatsByDate>();
if ( statsDict != null )
foreach ( var (key, value) in statsDict )
{
statsList.Add( new StatsByDate
{
ActualDate = key,
Statistics = value
} );
}
foreach ( var item in statsList )
{
Console.WriteLine( $"Date: {item.ActualDate}, Total Comm: {item.Statistics.Comments.Total}, Total Reactions: {item.Statistics.Reactions.Total}, Total pageViews: {item.Statistics.PageViews.Total}" );
}
internal record Comments
{
[JsonPropertyName("total")]
public int Total { get; set; } = 0;
}
internal record Follows
{
[JsonPropertyName("total")]
public int Total { get; set; } = 0;
}
internal record Reactions
{
[JsonPropertyName("total")]
public int Total { get; set; } = 0;
[JsonPropertyName("like")]
public int Like { get; set; } = 0;
[JsonPropertyName("readinglist")]
public int Readinglist { get; set; } = 0;
[JsonPropertyName("unicorn")]
public int Unicorn { get; set; } = 0;
}
internal record PageViews
{
[JsonPropertyName("total")]
public int Total { get; set; } = 0;
[JsonPropertyName("average_read_time_in_seconds")]
public int AverageReadTimeInSeconds { get; set; } = 0;
[JsonPropertyName("total_read_time_in_seconds")]
public int TotalReadTimeInSeconds { get; set; } = 0;
}
internal record Stats
{
[JsonPropertyName("comments")]
public Comments Comments { get; set; }
[JsonPropertyName("follows")]
public Follows Follows { get; set; }
[JsonPropertyName("reactions")]
public Reactions Reactions { get; set; }
[JsonPropertyName("page_views")]
public PageViews PageViews { get; set; }
}
internal record StatsByDate
{
public DateTime ActualDate { get; set; }
public Stats Statistics { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment