2017-03-02 59 views
0

我從Facebook得到這個JToken回:JToken價值爲String

JToken userInfo = await CurrentClient.InvokeApiAsync("/.auth/me", HttpMethod.Get, null); 

我需要的姓名和電子郵件出於此。 我試過幾種不同的方法。 有人可以幫忙嗎?

[{"access_token": "EAADnAW61F4UBAF1UBeVZCSfA5L4k9Ybx1Vi0qAZBLjZAsInqpwOfuHZCKo3yqrBoFJUfcBnr49eLlZAsxogZBZBLTVpFxAM1saXd3Dg5HobSDYoBvH3e2XAZAKGb756a6i1OVi705ZB6jOJl3rISjbOlzC2uSZAf3kwOe0GZAH385vjQAZDZD",  
"expires_on": "2017-04-30T02:17:25.7584847Z", 
"provider_name": "facebook", 

"user_claims": [ 
{"typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", 
"val": "10155043823259643"}, 

{"typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", 
"val": "[email protected]*****.net"},  

{"typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",   
"val": "Alex Marc"},  

{"typ": "urn:facebook:link",  
"val": "https://www.facebook.com/app_scoped_user_id/4327859432758270/"},  

{"typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname",   
"val": "Alexandre"},  

{"typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname",  
"val": "Marc"},  

{"typ": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/gender",  
"val": "male"},  

{"typ": "urn:facebook:locale",  
"val": "en_US"},  

{"typ": "urn:facebook:timezone",   
"val": "-5"}], 

"user_id": "[email protected]*****.net"}] 

回答

0

模型,然後使用JToken.ToObject:

var model = userInfo.ToObject<List<Model>>().First(); 
Console.WriteLine("UserID = " + model.UserID);    
Console.WriteLine("Firstname = " + model.UserClaims.First(p => p.ClaimType.Equals("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname")).Value); 
. 
. 
. 
public class Model { 
    [JsonProperty("user_claims")] 
    public List<UserClaim> UserClaims { get; set; } 
    [JsonProperty("user_id")] 
    public string UserID { get; set; } 
} 

public class UserClaim { 
    [JsonProperty("typ")] 
    public string ClaimType { get; set; } 
    [JsonProperty("val")] 
    public string Value { get; set; } 
} 
+0

感謝您的答覆。我正在嘗試你的代碼。它要求第二個Console.Writeline結束。看看.. [https://drive.google.com/open?id=0B_rEpmqjSkWsaTZjbU1nRG5GdWs] – Spinteractive

+0

謝謝!你解決了它。昨天晚上我有點累了,代碼沒有對齊。它工作完美。欣賞!! – Spinteractive