如何将字符串转换为Json格式?

时间:2013-10-17 11:13:37

标签: asp.net

我在字符串变量中捕获Http响应。我想将字符串转换为json。如何转换?

  string responseData;  
  HttpWebResponse response = request.GetResponse() as HttpWebResponse;  
   if (response.StatusCode == HttpStatusCode.OK)  
   {
         System.IO.StreamReader responseReader = new System.IO.StreamReader(request.GetResponse().GetResponseStream());
         responseData = responseReader.ReadToEnd();
   }

1 个答案:

答案 0 :(得分:0)

首先见previous discussion

如果您使用的是asp.net MVC,请尝试以下方式

 [HttpGet]
        public JsonResult GetValues()
        {
         string responseData;  
         HttpWebResponse response = request.GetResponse() as HttpWebResponse;  
         if (response.StatusCode == HttpStatusCode.OK)  
         {
         System.IO.StreamReader responseReader = new System.IO.StreamReader(request.GetResponse().GetResponseStream());
         responseData = responseReader.ReadToEnd();
         }                 
         return Json(new { data = responseData}, JsonRequestBehavior.AllowGet);
         }