我的字符串json
{"data":[{"id":"CAMVqgY1g1cdLU5anDL69Tt5pyRh51-qkyKMHWHgH2mAG+vn+xQ%40mail.gmail.com","content":"Coba ngirim email mohon diterimarn","judul":"coba 1","sender":""Aldo Erianda""},{"id":"CAMVqgY1Trb5ZShRxzoX%3D0xaVQs5-Psh0J8V3JwQYVevcr8i5WA%40mail.gmail.com","content":"sampai nga yarn","judul":"coba 2","sender":""Aldo Erianda""}]}
我想计数记录内"数据",并显示在控制台或richtextbox的记录。几个教程仍然让我难以理解。我该怎么一步步做呢?
这应该适用于您的具体情况。它不能推广到任何json字符串。
var json = "{"data":[{"id":"CAMVqgY1g1cdLU5anDL69Tt5pyRh51-qkyKMHWHgH2mAG+vn+xQ%40mail.gmail.com","content":"Coba ngirim email mohon diterimarn","judul":"coba 1","sender":"Aldo Erianda"},{"id":"CAMVqgY1Trb5ZShRxzoX%3D0xaVQs5-Psh0J8V3JwQYVevcr8i5WA%40mail.gmail.com","content":"sampai nga yarn","judul":"coba 2","sender":"Aldo Erianda"}]}";
var deserialized = JsonConvert.DeserializeObject<IDictionary<string, JArray>>(json);
JArray recordList = deserialized["data"];
foreach (JObject record in recordList)
{
Console.WriteLine("id: " + record["id"]);
Console.WriteLine("content: " + record["content"]);
Console.WriteLine("judul: " + record["judul"]);
Console.WriteLine("sender: " + record["sender"]);
}
Console.WriteLine("count: " + recordList.Count);
注意:我稍微修改了你的json通过转义所有的引号使其成为一个有效的c#字符串。此外,我认为你在发件人的价值周围有额外的报价。