我在字符串变量中捕获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();
}
答案 0 :(得分:0)
如果您使用的是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);
}