【发布时间】:2017-12-09 09:31:00
【问题描述】:
请检查下面的控制器。我必须在这个控制器内处理 Json 响应。我的目标是抓住“成功”值的真假。我如何从以下 json 响应中访问该值?我已经尝试过使用 JsSerializer 但没有运气。请检查图片以更好地理解。提前致谢。
查看模型:
public class GoogleRecaptcha
{
public bool success { get; set; }
public DateTime challenge_ts { get; set; }
public string hostname { get; set; }
}
public ActionResult DownloadProcess()
{
using (BlexzWebDbEntities db = new BlexzWebDbEntities())
{
//Validate Google recaptcha below
var Response = Request["g-recaptcha-response"];
string SecretKey = "6Lghu-MJoniMPXVf";
//var client = new WebClient();
var client = new RestClient("https://www.google.com/recaptcha/api/siteverify");
var request = new RestRequest(Method.POST);
request.Method = Method.POST;
request.Parameters.Clear();
request.AddParameter("secret", SecretKey);
request.AddParameter("response", Response);
var ResultFromGoogle = client.Execute(request).Content;
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
var r = jsSerializer.DeserializeObject(ResultFromGoogle);
return View();
}
}
【问题讨论】:
-
创建一个包含这 3 个属性(
bool success等)的模型并反序列化对您的模型的响应。 -var result = jsSerializer.DeserializeObject<YourModel>(ResultFromGoogle) -
我做了同样的事情,但是得到了像这张图片一样的错误 - ibb.co/jBSx0G 并添加了模型。请检查编辑
-
哎呀,对不起,我没有注意就粘贴了你的 - 它是
jsSerializer.Deserialize<YourModel>(ResultFromGoogle)
标签: asp.net-mvc