C#使用LitJson解析Json数据
作者:互联网
//接受MQ服务器返回的值
private void jieshou(string zhiling, string can1, string can2, string can3, string can4, string can5)
{
Console.Write("============================================="+"指令:" + zhiling + " can1=" + can1 + " can2=" + can2 + " can3=" + can3 + " can4=" + can4 + " can5=" + can5 + "\n");
if(can1=="0"&&can3==null){
Console.Write("对比分数不合格或服务器上没有这个人的人员信息");
}
else if (!can1.Equals("0")) {
Console.Write("服务没连接上!!!!");
}
else if (can1.Equals("0") && can3 != "" && can3 != null)
{
can3 = "cardId=" + can3;
//将can3的参数发送给服务器
byte[] ByteData = System.Text.Encoding.Default.GetBytes(can3);
//将数据发送给服务器,并返回json数据
string jieshou = PostData(path, ByteData);
Console.WriteLine("+++++++++++++++" + jieshou);
//使用LitJson的JsonData方法进行解析
JsonData deJson = JsonMapper.ToObject(jieshou);
//遍历返回的json数据
foreach (JsonData item in deJson)
{
//创建对象
User user = new User();
//名字
user.name = item["name"].ToString();
//Console.WriteLine(user.name = item["name"].ToString());
//身份证号码
user.cardId = item["cardId"].ToString();
//Console.WriteLine(user.name = item["cardId"].ToString());
//部门简称
user.DepartName = item["bumen"].ToString();
this.label2.Text = user.name + " " + user.DepartName;
//Console.WriteLine(user.name = item["bumen"].ToString());
}
}
}
//网络请求部分
public static string PostData(string url, byte[] postData)
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = postData.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(postData, 0, postData.Length);
newStream.Close();
// Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
return reader.ReadToEnd();
}
标签:Console,string,C#,name,item,Json,user,LitJson,can3 来源: https://www.cnblogs.com/liqipiao/p/10999548.html