解析Action返回的Json数据
Posted 我是谁的谁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解析Action返回的Json数据相关的知识,希望对你有一定的参考价值。
WebRequest和HttpClient的Get、post方式解析:
namespace Fractalist.Benz.Controllers { public class ResolveResultController : Controller { public void WebRequestPost() { HttpWebResponse response = null; try { string url = "http://localhost:8022/ResolveResult/PostTest"; dynamic expando = new System.Dynamic.ExpandoObject(); expando.name = "gw"; var jsonInfo = JsonConvert.SerializeObject(expando); byte[] byteArray = Encoding.UTF8.GetBytes(jsonInfo); //string result = new HttpClient().GetStringAsync(url).Result; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json"; request.ContentLength = byteArray.Length; Stream newStream = request.GetRequestStream(); newStream.Write(byteArray, 0, byteArray.Length); //写入参数 newStream.Close(); response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { response = (HttpWebResponse)ex.Response; } StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string textResponse = sr.ReadToEnd(); // 返回的数据 JsonResultInfo deinfo = JsonConvert.DeserializeObject<JsonResultInfo>(textResponse); var results = deinfo.Result; var message = deinfo.message; } public void HttpClientGet() { string url = "http://localhost:8022/ResolveResult/GetTest"; //封装请求参数 Task<string> response = new HttpClient().GetStringAsync(url + "?name=gw"); string result = response.Result; JsonResultInfo deinfo = JsonConvert.DeserializeObject<JsonResultInfo>(result); var results = deinfo.Result; var message = deinfo.message; } public async Task<ActionResult> HttpClientPost() { string url = "http://localhost:8022/ResolveResult/PostTest"; var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; using (var client = new HttpClient(handler)) { //第一种方式 var content = new FormUrlEncodedContent(new Dictionary<string, string>() { {"name","glw"} }); //第二种方式 //string jsonData = ReturnJson("name", "glw"); //var content = new StringContent(jsonData, Encoding.UTF8, "application/json"); //封装请求参数 var response = await client.PostAsync(url, content); string result = await response.Content.ReadAsStringAsync(); JsonResultInfo deinfo = JsonConvert.DeserializeObject<JsonResultInfo>(result); var results = deinfo.Result; var message = deinfo.message; return Content(message); } } [AllowAnonymous] [HttpPost] public JsonResult PostTest(string name) { string.Format(@"【class】:ResolveResultController,【method】:Test,【message】:name---{0}" , name).LogInfo(); return Json(new { Result = false, Message = name + "我是好人也是个坏人" }); } [AllowAnonymous] public JsonResult GetTest(string name) { return Json(new { Result = false, Message = name + "我是好人也是个坏人" }, JsonRequestBehavior.AllowGet); } /// <summary> /// 组装请求json /// </summary> /// <param name="code"></param> /// <param name="value"></param> /// <returns></returns> static string ReturnJson(string code, string value) { Hashtable hastable = new Hashtable(); hastable.Add(code, value); return Newtonsoft.Json.JsonConvert.SerializeObject(hastable); } } }
以上是关于解析Action返回的Json数据的主要内容,如果未能解决你的问题,请参考以下文章
使用easyui中的datagrid时,通过action能够返回json数据,但是,却不能在页面中显示数据
Struts2 Action接收POST请求JSON数据及其实现解析