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();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM