JToken值到字符串

时间:2017-03-02 01:37:41

标签: json facebook xamarin

我从Facebook回来了这个JToken:

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": "alex@*****.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": "alex@*****.net"}]

1 个答案:

答案 0 :(得分: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; }
}