Exemplo n.º 1
0
        public void Prase_JSON()
        {
            //1.发送json并获取返回的json
            Kd_Send();
            //try
            //{
            //    FileStream fs = new FileStream(@"C:\Users\sunkb.CHENZHU\Desktop\Receive_JSON.txt", FileMode.Open);//初始化文件流
            //    byte[] array = new byte[fs.Length];//初始化字节数组
            //    fs.Read(array, 0, array.Length);//读取流中数据到字节数组中
            //    fs.Close();//关闭流
            //    result = Encoding.UTF8.GetString(array);//将字节数组转化为字符串
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("Error:" + ex.Message, "Error");
            //}
            string jsonal = @"{""send"":" + param + ",";

            jsonal += @"""recive"":" + result + "}";

            //2.解析返回结果的JSON
            JsonData jd = JsonMapper.ToObject(result);

            br.result  = (bool)jd["result"];
            br.message = (string)jd["message"];
            br.status  = (string)jd["status"];
            JsonData jdItems = jd["data"];

            if (br.status == "200" && br.result == true)
            {
                for (int i = 0; i < jdItems.Count; i++)
                {
                    br.kuaidinum = (string)jdItems[i]["kuaidinum"];
                    if (result.Contains("returnNum"))
                    {
                        br.returnNum = (string)jdItems[i]["returnNum"];
                    }
                    else
                    {
                        br.returnNum = "";
                    }
                    if (result.Contains("childNum"))
                    {
                        br.childNum = (string)jdItems[i]["childNum"];
                    }
                    else
                    {
                        br.childNum = "";
                    }
                    if (result.Contains("bulkpen"))
                    {
                        br.bulkpen = (string)jdItems[i]["bulkpen"];
                    }
                    else
                    {
                        br.bulkpen = "";
                    }
                    if (result.Contains("orgCode"))
                    {
                        br.orgCode = (string)jdItems[i]["orgCode"];
                    }
                    else
                    {
                        br.orgCode = "";
                    }
                    if (result.Contains("orgName"))
                    {
                        br.orgName = (string)jdItems[i]["orgName"];
                    }
                    else
                    {
                        br.orgName = "";
                    }
                    if (result.Contains("destCode"))
                    {
                        br.destCode = (string)jdItems[i]["destCode"];
                    }
                    else
                    {
                        br.destCode = "";
                    }
                    if (result.Contains("destName"))
                    {
                        br.destName = (string)jdItems[i]["destName"];
                    }
                    else
                    {
                        br.destName = "";
                    }
                    if (result.Contains("orgSortingCode"))
                    {
                        br.orgSortingCode = (string)jdItems[i]["orgSortingCode"];
                    }
                    else
                    {
                        br.orgSortingCode = "";
                    }
                    if (result.Contains("orgSortingName"))
                    {
                        br.orgSortingName = (string)jdItems[i]["orgSortingName"];
                    }
                    else
                    {
                        br.orgSortingName = "";
                    }
                    if (result.Contains("destSortingCode"))
                    {
                        br.destSortingCode = (string)jdItems[i]["destSortingCode"];
                    }
                    else
                    {
                        br.destSortingCode = "";
                    }
                    if (result.Contains("destSortingName"))
                    {
                        br.destSortingName = (string)jdItems[i]["destSortingName"];
                    }
                    else
                    {
                        br.destSortingName = "";
                    }
                    if (result.Contains("orgExtra"))
                    {
                        br.orgExtra = (string)jdItems[i]["orgExtra"];
                    }
                    else
                    {
                        br.orgExtra = "";
                    }
                    if (result.Contains("destExtra"))
                    {
                        br.destExtra = (string)jdItems[i]["destExtra"];
                    }
                    else
                    {
                        br.destExtra = "";
                    }
                    if (result.Contains("pkgCode"))
                    {
                        br.pkgCode = (string)jdItems[i]["pkgCode"];
                    }
                    else
                    {
                        br.pkgCode = "";
                    }
                    if (result.Contains("pkgName"))
                    {
                        br.pkgName = (string)jdItems[i]["pkgName"];
                    }
                    else
                    {
                        br.pkgName = "";
                    }
                    if (result.Contains("road"))
                    {
                        br.road = (string)jdItems[i]["road"];
                    }
                    else
                    {
                        br.road = "";
                    }
                    if (result.Contains("qrCode"))
                    {
                        br.qrCode = (string)jdItems[i]["qrCode"];
                    }
                    else
                    {
                        br.qrCode = "";
                    }
                    if (result.Contains("orderNum"))
                    {
                        br.orderNum = (string)jdItems[i]["orderNum"];
                    }
                    else
                    {
                        br.orderNum = "";
                    }
                    if (result.Contains("expressCode"))
                    {
                        br.expressCode = (string)jdItems[i]["expressCode"];
                    }
                    else
                    {
                        br.expressCode = "";
                    }
                    if (result.Contains("expressName"))
                    {
                        br.expressName = (string)jdItems[i]["expressName"];
                    }
                    else
                    {
                        br.expressName = "";
                    }
                    br.templateurl = (string)jdItems[i]["templateurl"][0];
                    br.template    = (string)jdItems[i]["template"][0];
                }
                MessageBox.Show("提交成功!快递单号为:" + br.kuaidinum + ",请前往前台打印!", "发送成功!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Save_log(KdService.kdcompany + "发送正常!单号:" + br.kuaidinum, "", jsonal);
            }
            else
            {
                MessageBox.Show("快递单号获取失败,请重新输入!", "发送失败!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Save_log("发送数据格式错误!错误代码为:" + br.message, "", jsonal);
            }

            guid = System.Guid.NewGuid().ToString();

            //写入表KD_detail
            string sqlsend = @"insert into KD_detail (ID,PrintDate,cancle,issend,rec_name,rec_mobil,rec_tel,rec_printaddr,rec_company,send_name,send_mobil,send_tel,send_printaddr,send_company,exptype,remark,needchild,neeBack,needtemplate,createuser,department,kdcompany)
                               values(" + "'" + guid + "','" + DateTime.Now + "','0','0'," + "'" + Rec_name + "','" + Rec_mobile + "','" + Rec_tel + "','" + Rec_paddr + "','" + Rec_company + "','" + Send_name + "','" + Send_mobile + "','" + Send_tel + "','" + Send_paddr + "','" + Send_company + "'," + "'标准快递','" + Send_remark + "'" + ",'0','0','1','" + loginfo.userName + "','" + loginfo.userDepartMent + "','" + KdService.kdcompany + "'" + ")";

            KdSql.Save_Sql(sqlsend);
            //写入数据库,表KD_detailentry
            string sqlre = @"insert into KD_detailentry (Finterid,kuaidinum,bulkpen,orgcode,orgname,destcode,destname,orgsortingcode,orgsortingname,destsortingcode,destsortingname,orgextra,destextra,pkgcode,road,qrcode,orderNum,expresscode,expressname,templater,url,pkgname,jsonval) 
                             values(" + "'" + guid + "','" + br.kuaidinum + "','" + br.bulkpen + "','" + br.orgCode + "','" + br.orgName + "','" + br.destCode + "','" + br.destName + "','" + br.orgSortingCode + "','" + br.orgSortingName + "','" + br.destSortingCode + "','" + br.destSortingName + "','" + br.orgExtra + "','" + br.destExtra + "','" + br.pkgCode + "','" + br.road + "','" + br.qrCode + "','" + br.orderNum + "','" + br.expressCode + "','" + br.expressName + "','" + br.template + "','" + br.templateurl + "','" + br.pkgName + "','" + jsonal + "'" + ")";

            KdSql.Save_Sql(sqlre);
            //Print_Labels();
        }