2016-04-27 25 views
-2

我有一个从调用WebApi返回的json格式数据我想访问异常的值。从C#中的json格式对象获取值

这是response.data内容:

重点:UserEmailAdress
值:空

我怎样才能得到UserEmailAdress的价值?

enter image description here

这是我的方法,它返回我需要在这里有一定的逻辑取决于我找回在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

是什么内部异常说呢? – Versatile

+3

回应似乎已成功,因此没有例外,换句话说,它是空的。有什么问题? – ManoDestra

+0

@Versatile给我的问题增加了一张照片。 – Alma

回答

0

在调试器玩耍后,我发现,DeserializeObject返回Dictionary<string, object>,这样你就可以投你的反序列化对象到,和它的作品

var json = "{\"exception\":null,\"remoteRefNumber\":\"testremref\",\"userEmailAddress\":\"t‌​[email protected]\"}"; 

Dictionary<string, object> userObject = new JavaScriptSerializer().DeserializeObject(json) as Dictionary<string, object>; 
+0

我不知道我应该使用什么(json),因为我在对象中也有response.data和data。 – Alma

+0

@Alma那么,'response.Content.ReadAsStringAsync()。Result'看起来像什么? –

+0

“{\”exception \“:null,\”remoteRefNumber \“:\”testremref \“,\”userEmailAddress \“:\”[email protected] \“}” – Alma