我正在尝试通过 BOARD 对 JSON 进行分组,并对 likes_count 求和,但不知道如何解决这个问题,因为我只能通过首先循环访问 Root 来访问 Transaction 类?
public class Transaction
{
public string Post { get; set; }
public int board {get; set; }
public int Sent_from { get; set; }
public int likes_count { get; set; }
}
public class Root
{
public int Sent_to { get; set; }
public List<Transaction> Transactions { get; set; }
}
static async System.Threading.Tasks.Task Main(string[] args)
{
var json = "";
using (WebClient wc = new WebClient())
{
json = wc.DownloadString("xxxxxxx");
}
var obj = JsonConvert.DeserializeObject<List<Root>>(json);
foreach (var item in obj)
{
foreach (var child in item.Transactions)
{
// access sent_from, likes_count and post.
}
}
}
我通常会使用:
obj.GroupBy(t => t.post);
这是一个 JSON 示例:
Sent_to: X,
total_received_likes: X,
Transactions: [
{
Post: "50776785",
board: "600",
Sent_from: 359716,
likes_count: 4,
},
{
Post: "5085129785",
board: "500",
Sent_from: 359716,
likes_count: 6,
},
{
Post: "506542785",
board: "500",
Sent_from: 359716,
likes_count: 9,
},
在这种情况下的预期输出:
板 500 中有 15 个赞
4 个赞在板 600 中。