0

我的字符串 json

{"data":[{"id":"CAMVqgY1g1cdLU5anDL69Tt5pyRh51-qkyKMHWHgH2mAG+vn+xQ%40mail.gmail.com","content":"Coba ngirim email mohon diterima\r\n","judul":"coba 1","sender":"\"Aldo Erianda\""},{"id":"CAMVqgY1Trb5ZShRxzoX%3D0xaVQs5-Psh0J8V3JwQYVevcr8i5WA%40mail.gmail.com","content":"sampai nga ya\r\n","judul":"coba 2","sender":"\"Aldo Erianda\""}]}

我想计算“数据”中的记录,并在控制台或富文本框中显示记录。几个教程仍然让我难以弄清楚。我应该如何一步一步做?

4

1 回答 1

0

This should work for your specific case. It's not generalizable to any json string.

var json = "{\"data\":[{\"id\":\"CAMVqgY1g1cdLU5anDL69Tt5pyRh51-qkyKMHWHgH2mAG+vn+xQ%40mail.gmail.com\",\"content\":\"Coba ngirim email mohon diterima\r\n\",\"judul\":\"coba 1\",\"sender\":\"Aldo Erianda\"},{\"id\":\"CAMVqgY1Trb5ZShRxzoX%3D0xaVQs5-Psh0J8V3JwQYVevcr8i5WA%40mail.gmail.com\",\"content\":\"sampai nga ya\r\n\",\"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);

Note: I slightly modified your json by escaping all the quotations to make it a valid C# string. Also, I think you have extra quotations around the value of your sender.

于 2013-04-29T05:02:58.077 回答