Visual Studio C#如何解析和列出json Web服务的每个记录/字段

Visual Studio C#如何解析和列出json Web服务的每个记录/字段,c#,json,visual-studio,C#,Json,Visual Studio,下面是我为测试web服务收到的json文本,并查看如何显示每个记录字段 {“记录”:[ {“id”:“10”,“电子邮件”:”bcomecomaaacomea@myhost.om,“名称”:“点”},{“id”:“855”,“电子邮件”:webcastpoa0@myhost,“姓名”:“姓名”},{“id”:“856”,“电子邮件”:webcastpoa1@myhost,“姓名”:“姓名”},{“id”:“857”,“电子邮件”:webcastpoa2@myhost,“姓名”:“姓名”},{“i

下面是我为测试web服务收到的json文本,并查看如何显示每个记录字段

{“记录”:[ {“id”:“10”,“电子邮件”:”bcomecomaaacomea@myhost.om,“名称”:“点”},{“id”:“855”,“电子邮件”:webcastpoa0@myhost,“姓名”:“姓名”},{“id”:“856”,“电子邮件”:webcastpoa1@myhost,“姓名”:“姓名”},{“id”:“857”,“电子邮件”:webcastpoa2@myhost,“姓名”:“姓名”},{“id”:“858”,“电子邮件”:webcastpoa3@myhost,“姓名”:“姓名”},{“id”:“859”,“电子邮件”:webcastpoa4@myhost,“名称”:“名称4”},{“id”:“860”,“电子邮件”:webcastpoa5@myhost,“姓名”:“姓名”},{“id”:“861”,“电子邮件”:webcastpoa6@myhost,“姓名”:“姓名”},{“id”:“862”,“电子邮件”:webcastpoa7@myhost,“姓名”:“姓名”},{“id”:“863”,“电子邮件”:webcastpoa8@myhost,“姓名”:“姓名”},{“id”:“864”,“电子邮件”:webcastpoa9@myhost,“姓名”:“姓名9”},{“id”:“865”,“电子邮件”:webcastpoa10@myhost,“名称”:“名称10”},{“id”:“866”,“电子邮件”:webcastpoa11@myhost,“姓名”:“姓名11”},{“id”:“867”,“电子邮件”:webcastpoa12@myhost,“姓名”:“姓名”},{“id”:“868”,“电子邮件”:webcastpoa13@myhost,“姓名”:“姓名13”},{“id”:“869”,“电子邮件”:webcastpoa14@myhost,“姓名”:“姓名”},{“id”:“870”,“电子邮件”:webcastpoa15@myhost,“姓名”:“姓名”},{“id”:“871”,“电子邮件”:webcastpoa16@myhost,“名称”:“name_16”},{“id”:“872”,“email”:webcastpoa17@myhost,“姓名”:“姓名17”},{“id”:“873”,“电子邮件”:webcastpoa18@myhost,“姓名”:“姓名”},{“id”:“874”,“电子邮件”:webcastpoa19@myhost,“姓名”:“姓名19”},{“id”:“875”,“电子邮件”:webcastpoa20@myhost,“姓名”:“姓名20”},{“id”:“876”,“电子邮件”:webcastpoa21@myhost,“姓名”:“姓名”},{“id”:“877”,“电子邮件”:"webcastpoa22@myhost,“姓名”:“姓名”},{“id”:“878”,“电子邮件”:webcastpoa23@myhost,“姓名”:“姓名23”},{“id”:“879”,“电子邮件”:webcastpoa24@myhost,“姓名”:“姓名”},{“id”:“880”,“电子邮件”:webcastpoa25@myhost,“姓名”:“姓名”},{“id”:“881”,“电子邮件”:webcastpoa26@myhost,“姓名”:“姓名”},{“id”:“882”,“电子邮件”:webcastpoa27@myhost,“name”:“name_27”},{“id”:883,“电子邮件”:webcastpoa28@myhost,“姓名”:“姓名”},{“id”:“884”,“电子邮件”:webcastpoa29@myhost,“姓名”:“姓名29”},{“id”:“885”,“电子邮件”:webcastpoa30@myhost,“姓名”:“姓名”},{“id”:“886”,“电子邮件”:webcastpoa31@myhost,“姓名”:“姓名”},{“id”:“887”,“电子邮件”:webcastpoa32@myhost,“姓名”:“姓名”},{“id”:“888”,“电子邮件”:webcastpoa33@myhost,“名称”:姓名{“id”:“889”,“电子邮件”:webcastpoa34@myhost,“姓名”:“姓名”},{“id”:“890”,“电子邮件”:webcastpoa35@myhost,“姓名”:“姓名”},{“id”:“891”,“电子邮件”:webcastpoa36@myhost,“姓名”:“姓名”},{“id”:“892”,“电子邮件”:webcastpoa37@myhost,“姓名”:“姓名”},{“id”:“893”,“电子邮件”:webcastpoa38@myhost,“姓名”:“姓名”},{“id”:“894”,“电子邮件”:webcastpoa39@myhost,“姓名”:“姓名”},{“id”:“895”,“电子邮件”:webcastpoa40@myhost,“姓名”:“姓名”},{“id”:“896”,“电子邮件”:webcastpoa41@myhost,“姓名”:“姓名”},{“id”:“897”,“电子邮件”:webcastpoa42@myhost,“姓名”:“姓名42”},{“id”:“898”,“电子邮件”:webcastpoa43@myhost,“姓名”:“姓名”},{“id”:“899”,“电子邮件”:webcastpoa44@myhost,“名称”:“名称”} ]}

到目前为止,我有以下代码

using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Net;
using System.IO;
using Newtonsoft.Json;

WebClient client = new WebClient();
string reply = client.DownloadString("http://192.168.1.115/php_poa/test_select.php");
MessageBox.Show(reply);

records p1 = JsonConvert.DeserializeObject<records>(reply);
MessageBox.Show(p1.ToString());


class records
{
    public string id { get; set; }
    public string email { get; set; }
    public string name { get; set; }
}
使用System.Windows.Forms;
使用MySql.Data.MySqlClient;
Net系统;
使用System.IO;
使用Newtonsoft.Json;
WebClient客户端=新的WebClient();
string reply=client.DownloadString(“http://192.168.1.115/php_poa/test_select.php");
MessageBox.Show(回复);
记录p1=JsonConvert.DeserializeObject(应答);
Show(p1.ToString());
课堂记录
{
公共字符串id{get;set;}
公共字符串电子邮件{get;set;}
公共字符串名称{get;set;}
}
并希望使用for循环来处理每条记录

谢谢

只需使用
列表
即可。您无需通过循环对其进行迭代

示例:

List<records> p1 = JsonConvert.DeserializeObject<List<records>>(reply);
只需使用
List
即可。您不需要在循环中迭代它

示例:

List<records> p1 = JsonConvert.DeserializeObject<List<records>>(reply);

您需要使用下一个类进行反序列化:

class MyResponse
{
    public List<Records> Records { get; set; }
}

class Records
{
    public string Id { get; set; }
    public string Email { get; set; }
    public string Name { get; set; }
}
类MyResponse
{
公共列表记录{get;set;}
}
课堂记录
{
公共字符串Id{get;set;}
公共字符串电子邮件{get;set;}
公共字符串名称{get;set;}
}
现在,您可以反序列化web响应:

MyResponse myResponse = JsonConvert.DeserializeObject<MyResponse>(reply);
MyResponse MyResponse=JsonConvert.DeserializeObject(reply);
您需要使用下一个类进行反序列化:

class MyResponse
{
    public List<Records> Records { get; set; }
}

class Records
{
    public string Id { get; set; }
    public string Email { get; set; }
    public string Name { get; set; }
}
类MyResponse
{
公共列表记录{get;set;}
}
课堂记录
{
公共字符串Id{get;set;}
公共字符串电子邮件{get;set;}
公共字符串名称{get;set;}
}
现在,您可以反序列化web响应:

MyResponse myResponse = JsonConvert.DeserializeObject<MyResponse>(reply);
MyResponse MyResponse=JsonConvert.DeserializeObject(reply);
要遍历循环,可以执行以下操作

 JObject records = JObject.Parse(json);

 foreach (var record in obj["records"])
{
  records p1 = JsonConvert.DeserializeObject<records>(reply);
}
和使用
Records record=new Records(record)
而不是循环中的
反序列化对象
要循环,可以执行以下操作

 JObject records = JObject.Parse(json);

 foreach (var record in obj["records"])
{
  records p1 = JsonConvert.DeserializeObject<records>(reply);
}
和使用
记录记录=新记录(记录)而不是<代码> DeXialIZeObjult<代码> >在循环

中,您应该创建一个表示这个JSON的类,并将该JSON反序列化为该类的实例,您应该考虑将类重命名为<代码>记录>代码>,以表示单个记录,而不是一组记录。任何记录都有名称、邮件和ID。表示JSON的类,并将该JSON反序列化为该类的实例,您应该考虑将类重命名为<代码>记录>代码>,以表示单个记录,而不是一组记录。