我通过调用WebApi返回了json格式数据我希望能够访问异常值。
这是response.data
:
键:UserEmailAdress
值:空
如何获取UserEmailAdress的值?
这是我的方法,它返回我需要有一些逻辑的对象,这取决于我在UserEmailAdress中返回的值。
[HttpPost]
public async Task<ActionResult> Search(UserInfo userInfo)
{
HttpClient client = new HttpClient();
object userObject = null;
if (userInfo.LastName != null && userInfo.Zip != null && userInfo.Ssn != null)
{
string accessKey = CreateAccountKey(userInfo.LastName, userInfo.Zip, userInfo.Ssn);
var response = await client.GetAsync(string.Format("{0}{1}/{2}", baseUrl, "/verifyuser", accessKey));
if (response.IsSuccessStatusCode)
{
userObject = new JavaScriptSerializer().DeserializeObject(response.Content.ReadAsStringAsync().Result) as object;
}
}
var respone = new
{
success = userObject != null,
data = userObject
};
return Json(respone);
}
答案 0 :(得分:0)
在调试器中玩游戏后,我发现DeserializeObject
返回Dictionary<string, object>
,因此您可以将反序列化的对象转换为该对象,并且它可以正常工作
var json = "{\"exception\":null,\"remoteRefNumber\":\"testremref\",\"userEmailAddress\":\"test@yahoo.com\"}";
Dictionary<string, object> userObject = new JavaScriptSerializer().DeserializeObject(json) as Dictionary<string, object>;