C# Json 常用
1、MVC返回json方式
1、(自定义返回json对象)var optionStrCity=“”;var optionStrBank =“”;
return Json(new { optionStrCity = optionStrCity, optionStrBank = optionStrBank });
2、(MVC 对象Json序列化) Object obj=new Object();
return Json(obj,JsonBehavier.AllowGet);
2、Json 接收
1、(自动映射)
var postData=$.toJSON(obj);
List<ContractPostModel> ModelList = JsonConvert.DeserializeObject<List<ContractPostModel>>(System.Web.HttpContext.Current.Request.Form["postData"]);
3、C# post 数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#region 调取接口 post 数据 JavaScriptSerializer jss = new JavaScriptSerializer(); string JsonStr = "JsonDetail=" + jss.Serialize(AppLyDetail); string AddUrl = "http://jrpt.fang.com/managerproduct/DataMigrate/AddOnepointzero"; byte[] postData = Encoding.UTF8.GetBytes(JsonStr);//编码,尤其是汉字,事先要看下抓取网页的编码方式 WebClient webClient = new WebClient(); webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可 try { byte[] responseData = webClient.UploadData(AddUrl, "POST", postData);//得到返回字符流 string srcString = Encoding.UTF8.GetString(responseData);//解码 if (!string.IsNullOrWhiteSpace(srcString)) { if (srcString != "error") { if (OrderType == 1) { Finance_LoanAppPerBLL.UpdateLoanInfoIsTongBu(AppLyDetail.ID, 1, AppLyDetail.OrderNo); } else { Finance_LoanAppAgentBLL.UpdateLoanInfoIsTongBu(AppLyDetail.ID, 1, AppLyDetail.OrderNo); } WriteLog(AppLyDetail.OrderNo + "------执行成功"); return 1; } else { WriteLog(AppLyDetail.OrderNo + "------同步失败"); return 0; } } else { WriteLog(AppLyDetail.OrderNo + "------同步失败"); return 0; } } catch (Exception) { WriteLog(AppLyDetail.OrderNo + "------执行异常"); return 0; } #endregion
4、C# 模拟http请求(POST)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private string HttpPost(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr); request.CookieContainer = cookie; Stream myRequestStream = request.GetRequestStream(); StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")); myStreamWriter.Write(postDataStr); myStreamWriter.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); response.Cookies = cookie.GetCookies(response.ResponseUri); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; }
5、C# 模拟http请求(GET)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public string HttpGet(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; }