Home > Net >  C # the MVC controller method, how to receive a JSON array parameter, I write code that receive less
C # the MVC controller method, how to receive a JSON array parameter, I write code that receive less

Time:09-29

//is called interface, receive and parse the JSON parameter
Public ActionResult ParseJson (string RZSJ, string LDSJ, List Param)
{
//processing param, I can't get a param here the value of the display is empty, can't use a List or a string

Return the Content (" RZSJ: "+ +", LDSJ: "+ LDSJ RZSJ);
}

//using Http Post simulation call interface
Public ActionResult TestPostJson2 ()
{
//the JSON format into the parameter
Param string param="{" \ ": {" FXBM \ ": [{" FXBM \ ": " GK \ "}, {\ "FXBM " : \ "fk1 - FK "}, {\ "FXBM " : \ "HK \}], " FTBM \ ": [{" FTBM \ ": " VC \ "}, {\ "FTBM " : \ "VI "}, {\ "FTBM " : \ "VD \}]}, " RZSJ \ ": " 2019-10-30 \ ", \ "LDSJ " : \ "2019-10-31 "} ";

The string result=Post (" http://localhost:19992/PMS/ParseJson ", param);
Return the Content (" result: "+ result);
}


Public string Post (Url string, the string jsonParas)
{
String strURL=Url;
//create an HTTP request
HttpWebRequest request=(HttpWebRequest) WebRequest. Create (strURL);
//Post request way
Request. The Method="POST";
//content type
Request. ContentType="application/json";

//set parameters and URL encoding

String paraUrlCoded=jsonParas;//System. Web. HttpUtility. UrlEncode (jsonParas);

Byte [] content;
//convert Json string into bytes
Content=System. Text. Encoding. UTF8. GetBytes (paraUrlCoded);
//set the request ContentLength
Request, ContentLength=payload Length;
//send the request, the request flow

The Stream writer;
Try
{
Writer.=the request GetRequestStream ();//get used to write the request data Stream object
}
The catch (Exception)
{
Writer=null;
The Console. Write (" connect to the server failed!" );
}
//write request parameter inflow
Writer. Write (payload, 0, payload. Length);
Writer. The Close ();//close the request flow
//String strValuehttps://bbs.csdn.net/topics/="";//strValue into the HTTP response stream of characters returned by
HttpWebResponse response;
Try
{
//get the response flow
The response=(HttpWebResponse) request. The method GetResponse ();
}
The catch (WebException ex)
{
The response=ex. The response as HttpWebResponse;
}
The Stream s=response. GetResponseStream ();
//the Stream postData=https://bbs.csdn.net/topics/Request.InputStream;
StreamReader sRead=new StreamReader (s);
String postContent=sRead. ReadToEnd ();
SRead. Close ();
Return postContent;//return the Json data
}

CodePudding user response:

https://www.cnblogs.com/Sea1ee/p/10438838.html

CodePudding user response:

Himself wrote a class to as method parameters

CodePudding user response:

Into a ParseJson () request. The form if not "param" took to the value is the problem,,,

CodePudding user response:

The string result=Post (" http://localhost:19992/PMS/ParseJson ", param);
This post method, you change the parameter name of param to json, then your json=param,
In the PMS/ParseJson, the method of the Controller is as follows, please refer to use:
 
[HttpPost]
Public ActionResult ParseJson (string json)
{
Dynamic model=Newtonsoft. Json. Linq. JToken. Parse (Json) as the dynamic;//parsing
String v=model. LDSJ;//value directly LDSJ
return View();
}

CodePudding user response:

Because of you POST is just a string
  • Related