C# 接口调用并解析json字符串
2022/2/26 12:22:11
本文主要是介绍C# 接口调用并解析json字符串,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.Post方式接收json格式并查找相应带list的数据返回
/// <summary> /// 调用Lot卡打印接口 /// </summary> /// <param name="project"></param> /// <returns></returns> /// {"PN":"W**CS56DM7-00","cuPNList":[{"cuPN":"900000000047578"},{"cuPN":"900000000047579"},{"cuPN":"900000000047577"},{"cuPN":"9A4000000009443"}]} [HttpPost] [Route("api/GetLotCardMIMAFInfo")] public HttpResponseMessage GetLotCardMIMAFInfo() { var data = Request.Content.ReadAsStringAsync().Result; var PNlist = ""; JObject jsonObj = JObject.Parse(data); string PNno = jsonObj["PN"].ToString(); JArray jsonAry = JArray.Parse(jsonObj["cuPNList"].ToString()); JObject stu1Obj = JObject.Parse(jsonAry[0].ToString()); string cuPN = stu1Obj["cuPN"].ToString(); foreach (var ss in jsonAry) //查找某个字段与值 { PNlist = PNlist + ss["cuPN"].ToString() + ","; // ((JObject)ss)["cuPN"]; // JObject.Parse(jsonAry[0].ToString()); } JsonData jd = new JsonData(); DataTable dt = checkInplanservice.GettLotCardInfoTable(PNno); DataTable dtMAF = checkInplanservice.GettLotCardMAFInfoTable(PNlist); //OBJ转化成JSON jd.Status = HandleStatus.Success; if (dt.Rows.Count == 0) { jd.Status = HandleStatus.Fail; jd.Msg = "无数据"; } else { jd.Data = new { cuLayerType = dt.Rows[0]["LayerLevel"], cuCustomer = dt.Rows[0]["MiCustPn"], cuCustomerName = dt.Rows[0]["MiCustName"], cuQRCustomerPN = "", cuQRCustomerPNVersion = "", cuPNLOrigin = dt.Rows[0]["lvdr"], cuSize = dt.Rows[0]["Value"], cuCurrSize = dt.Rows[0]["MiSetArea"], cuHSFRequirement = dt.Rows[0]["AttrCode"], cuFinalThickness = dt.Rows[0]["cuFinalThickness"], cuPNList = dtMAF }; } string msgAll = JsonConvert.SerializeObject(jd); return JsonResult(msgAll); } public HttpResponseMessage JsonResult(string msg) { HttpResponseMessage result = new HttpResponseMessage(); result.Content = new StringContent(msg, Encoding.GetEncoding("UTF-8"), "application/json"); return result; } public HttpResponseMessage JsonResult(object obj) { HttpResponseMessage result = new HttpResponseMessage(); result.Content = new StringContent(JsonConvert.SerializeObject(obj), Encoding.GetEncoding("UTF-8"), "application/json"); return result; }
2.外部调用接口并更新数据
// /// <summary> /// 更改设备OnHold状态 /// </summary> /// <returns></returns> /// {"OS":"create","mcnrs":[{"oano":"abc123","equipid":"A10001"},{"oano":"abc123","equipid":"A10002"}]} [HttpPost] public ActionResult MEEequipOHStatus() //HttpResponseMessage { Logger.Error(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "--" + "update"); ArrayList mess = new ArrayList(); BaseManager bm = new BaseManager(); OperationResult result = new OperationResult(); try { var request = System.Web.HttpContext.Current.Request; byte[] requestData = new byte[request.InputStream.Length]; request.InputStream.Read(requestData, 0, (int)request.InputStream.Length); var jsonData = Encoding.UTF8.GetString(requestData); JObject jsonObj = JObject.Parse(jsonData); string OS = jsonObj["OS"].ToString(); JArray jsonAry = JArray.Parse(jsonObj["mcnrs"].ToString()); var msg = ""; if (OS.ToLower() != "create") { foreach (var ss in jsonAry) //查找某个字段与值 { string sql = string.Format(@"update [t_EquipOAOH] set [Stauts] = 'UP',Updatedate='" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where Oano = '" + ss["oano"].ToString() + "'"); bm.ExecuteCommand(sql); msg += "EquipOAOH:" + ss["oano"].ToString() + " complete sucess;"; Logger.Error(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "--" + "complete:" + msg + ""); } } else { foreach (var ss in jsonAry) //查找某个字段与值 { string sql = string.Format(@"insert into t_EquipOAOH ([Oano],[EquipID] ,[Stauts] ) values ( '" + ss["oano"].ToString() + "','" + ss["equipid"].ToString() + "','OH')"); bm.ExecuteCommand(sql); msg += "EquipOAOH:" + ss["equipid"].ToString() + " insert sucess;"; Logger.Error(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "--" + "Success:" + msg + ""); } } result.Data = msg; } catch (Exception e) { result.Errors.Add("Error:" + e.Message); } return Json(result); }
这篇关于C# 接口调用并解析json字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#