-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentLoader.cs
More file actions
40 lines (31 loc) · 1.1 KB
/
Copy pathCommentLoader.cs
File metadata and controls
40 lines (31 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
namespace RYCBEditorX.Utils;
public class CommentLoader
{
public static async Task<List<Comment>> LoadCommentsAsync(string filePath)
{
var comments = new List<Comment>();
// 读取 JSON 文件内容
var jsonString = await File.ReadAllTextAsync(filePath);
// 解析 JSON
var jsonDocument = JsonDocument.Parse(jsonString);
foreach (var element in jsonDocument.RootElement.EnumerateObject())
{
var userName = element.Name;
var commentObject = element.Value;
var comment = new Comment
{
User = userName,
Uid = commentObject.GetProperty("Uid").GetString(),
CommentText = commentObject.GetProperty("Comment").GetString(),
Time = commentObject.GetProperty("Time").GetString(),
Likes = commentObject.GetProperty("Likes").GetInt32()
};
comments.Add(comment);
}
return comments;
}
}