C# Newtonsoft.Json.dll json数据格式上传下载解析
Posted 云辰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# Newtonsoft.Json.dll json数据格式上传下载解析相关的知识,希望对你有一定的参考价值。
前提需要导入Newtonsoft.Json.dll
1.创建实体输入json参数对象
[JsonObject(MemberSerialization.OptOut)] class GateSignInputInfo { /// <summary> /// 考场代号 /// </summary> public string kcdh { get; set; } = string.Empty; /// <summary> /// 查询当前时间后数据 /// </summary> public string gxsj { get; set; } = string.Empty; }
2.创建实体Json返回参数对象
[JsonObject(MemberSerialization.OptOut)] class ResultInfo { /// <summary> /// 返回码 /// 0失败1成功 /// </summary> public int errorcode { get; set; } = 0; /// <summary> /// 提示信息 /// </summary> public string message { get; set; } /// <summary> /// 数据信息 /// </summary> public object data { get; set; } }
3.创建json返回对象内的数据
[JsonObject(MemberSerialization.OptOut)] class GateSignOutputInfo { /// <summary> /// 签到序号 /// </summary> public string qdxh { get; set; } /// <summary> /// 姓名 /// </summary> public string xm { get; set; } /// <summary> /// 身份证明号码 /// </summary> public string sfzmhm { get; set; } /// <summary> /// 签到时间 /// </summary> public string qdsj { get; set; } }
4.将Json数据序列化
// 生成JSON数据
GateSignInputInfo inputInfo = new GateSignInputInfo
{
kcdh = this.Kcdh
};
string json = JsonConvert.SerializeObject(inputInfo);
5.构建Json请求,传送数据
#region[构建请求,获取数据]
/// <summary>
/// 根据url地址传递json数据
/// </summary>
/// <param name="url">目标地址</param>
/// <param name="json">json数据</param>
/// <param name="output">返回json数据</param>
/// <returns>成功响应为true</returns>
private bool UploadResult(string url, string json, out string output) { try { HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/json"; using (Stream requestStream = request.GetRequestStream()) { using (StreamWriter sw = new StreamWriter(requestStream, Encoding.GetEncoding("GBK"))) //using (StreamWriter sw = new StreamWriter(requestStream)) { sw.Write(json); } } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(responseStream)) { output = sr.ReadToEnd(); } } } catch (Exception ex) { MessageBox.Show($"访问失败:{url} \\r\\n{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); output = ex.Message; return false; } return true; } #endregion
6.反序列Json数据,解析数据
if (UploadResult(this.RequestByKS, json, out string output)) { ResultInfo resultInfo = JsonConvert.DeserializeObject<ResultInfo>(output); if (resultInfo.errorcode == 0) { labelHelp.Text = $"{resultInfo.message}"; return; } else { List<GateSignOutputInfo> outputInfos = JsonConvert.DeserializeObject<List<GateSignOutputInfo>>(resultInfo.data.ToString()); GateSignOutputInfo info = outputInfos.Find(x => x.sfzmhm == textPersonalCard.Text); if(info == null) { labelHelp.Text = $"尚未通过门禁,无法录入"; return; } } }
以上是关于C# Newtonsoft.Json.dll json数据格式上传下载解析的主要内容,如果未能解决你的问题,请参考以下文章
Asp.net C# 使用Newtonsoft.Json 实现DataTable转Json格式数据
“Newtonsoft.Json.JsonConvert”类型存在于“Newtonsoft.Json.dll”和“NuGetApi2.dll”中
c# 读取json的问题,JObject不能强转成JArray