// 被调用接口,接收和解析JSON参数
public ActionResult ParseJson(string RZSJ, string LDSJ, List<Dictionary<string, string>> param)
{
//处理param,我在这里接收不到param的值,显示为空,用List或string都不行 return Content("RZSJ:" + RZSJ + ",LDSJ:" + LDSJ);
} //用Http Post模拟调用接口
public ActionResult TestPostJson2()
{
//JSON格式入参
string param = "{\"param\":{\"FXBM\":[{\"FXBM\":\"GK\"},{\"FXBM\":\"FK\"},{\"FXBM\":\"HK\"}],\"FTBM\":[{\"FTBM\":\"VC\"},{\"FTBM\":\"VI\"},{\"FTBM\":\"VD\"}]},\"RZSJ\":\"2019-10-30\",\"LDSJ\":\"2019-10-31\"}"; string result = Post("http://localhost:19992/PMS/ParseJson", param);
return Content("result:" + result);
}
public string Post(string Url, string jsonParas)
{
string strURL = Url;
//创建一个HTTP请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
//Post请求方式
request.Method = "POST";
//内容类型
request.ContentType = "application/json"; //设置参数,并进行URL编码 string paraUrlCoded = jsonParas;//System.Web.HttpUtility.UrlEncode(jsonParas); byte[] payload;
//将Json字符串转化为字节
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
//设置请求的ContentLength
request.ContentLength = payload.Length;
//发送请求,获得请求流 Stream writer;
try
{
writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
}
catch (Exception)
{
writer = null;
Console.Write("连接服务器失败!");
}
//将请求参数写入流
writer.Write(payload, 0, payload.Length);
writer.Close();//关闭请求流
// String strValue = "";//strValue为http响应所返回的字符流
HttpWebResponse response;
try
{
//获得响应流
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = ex.Response as HttpWebResponse;
}
Stream s = response.GetResponseStream();
// Stream postData = Request.InputStream;
StreamReader sRead = new StreamReader(s);
string postContent = sRead.ReadToEnd();
sRead.Close();
return postContent;//返回Json数据
}
public ActionResult ParseJson(string RZSJ, string LDSJ, List<Dictionary<string, string>> param)
{
//处理param,我在这里接收不到param的值,显示为空,用List或string都不行 return Content("RZSJ:" + RZSJ + ",LDSJ:" + LDSJ);
} //用Http Post模拟调用接口
public ActionResult TestPostJson2()
{
//JSON格式入参
string param = "{\"param\":{\"FXBM\":[{\"FXBM\":\"GK\"},{\"FXBM\":\"FK\"},{\"FXBM\":\"HK\"}],\"FTBM\":[{\"FTBM\":\"VC\"},{\"FTBM\":\"VI\"},{\"FTBM\":\"VD\"}]},\"RZSJ\":\"2019-10-30\",\"LDSJ\":\"2019-10-31\"}"; string result = Post("http://localhost:19992/PMS/ParseJson", param);
return Content("result:" + result);
}
public string Post(string Url, string jsonParas)
{
string strURL = Url;
//创建一个HTTP请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
//Post请求方式
request.Method = "POST";
//内容类型
request.ContentType = "application/json"; //设置参数,并进行URL编码 string paraUrlCoded = jsonParas;//System.Web.HttpUtility.UrlEncode(jsonParas); byte[] payload;
//将Json字符串转化为字节
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
//设置请求的ContentLength
request.ContentLength = payload.Length;
//发送请求,获得请求流 Stream writer;
try
{
writer = request.GetRequestStream();//获取用于写入请求数据的Stream对象
}
catch (Exception)
{
writer = null;
Console.Write("连接服务器失败!");
}
//将请求参数写入流
writer.Write(payload, 0, payload.Length);
writer.Close();//关闭请求流
// String strValue = "";//strValue为http响应所返回的字符流
HttpWebResponse response;
try
{
//获得响应流
response = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = ex.Response as HttpWebResponse;
}
Stream s = response.GetResponseStream();
// Stream postData = Request.InputStream;
StreamReader sRead = new StreamReader(s);
string postContent = sRead.ReadToEnd();
sRead.Close();
return postContent;//返回Json数据
}
这个post的方法里,你把param的参数名改为json,那你的json=param。
在PMS/ParseJson,这个Controller的方法如下,请参考使用:[HttpPost]
public ActionResult ParseJson(string json)
{
dynamic model = Newtonsoft.Json.Linq.JToken.Parse(json) as dynamic;//解析
string v = model.LDSJ;//直接取值LDSJ
return View();
}