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

      

  • 相关阅读:
    【原】iOS学习之XML与JSON两种数据结构比较和各自底层实现
    ios 10 访问设置问题
    蛇形输出
    苹果内购流程详解
    iOS多线程比较
    App iCON 尺寸
    学习网站
    c++ lesson 一(命名空间输入输出)
    iOS中WebSocket的使用
    MAC TXT文本
  • 原文地址:https://www.cnblogs.com/Depingblogs/p/15938800.html
Copyright © 2020-2023  润新知