2017-01-07 46 views
0

所以,我試圖檢索用戶養活喜歡的第25條記錄如下:爲什麼動態Json沒有映射到我的類屬性值?爲什麼檢索Json無效?

var access_token = HttpContext.Items["access_token"].ToString(); 
      if (!string.IsNullOrEmpty(access_token)) 
      { 
       var appsecret_proof = access_token.GenerateAppSecretProof(); 

       var fb = new FacebookClient(access_token); 

       dynamic myFeed = await fb.GetTaskAsync(
        ("me/feed?fields=likes{{name,pic_large}}") 
//GraphAPICall formats the json retrieved from facebook 
         .GraphAPICall(appsecret_proof)); 

       string feed = myFeed; 

       var postList = new List<FBAnalyseViewModel>(); 
       foreach (dynamic post in myFeed.data) 
       { 
        postList.Add(DynamicExtension.ToStatic<FBAnalyseViewModel>(post)); 
       } 

以上GraphAPI獲取基本GraphAPICall然後格式化從Facebook獲取的Json和此處追加appsecret_proof加上參數如下:

public static string GraphAPICall(this string baseGraphApiCall, params object[] args) 
{ 
     //returns a formatted Graph Api Call with a version prefix and appends a query string parameter containing the appsecret_proof value 
     if (!string.IsNullOrEmpty(baseGraphApiCall)) 
     { 
      if (args != null && 
       args.Count() > 0) 
      { 
       //Determine if we need to concatenate appsecret_proof query string parameter or inject it as a single query string paramter 
       string _graphApiCall = string.Empty; 
       if (baseGraphApiCall.Contains("?")) 
        _graphApiCall = string.Format(baseGraphApiCall + "&appsecret_proof={" + (args.Count() - 1) + "}", args); 
       else 
        _graphApiCall = string.Format(baseGraphApiCall + "?appsecret_proof={" + (args.Count() - 1) + "}", args); 

        //prefix with Graph API Version 
        return string.Format("v2.8/{0}", _graphApiCall); 
       } 
       else 
        throw new Exception("GraphAPICall requires at least one string parameter that contains the appsecret_proof value."); 
     } 
     else 
      return string.Empty; 
} 

我使用自動映射技術,爲我的屬性值如下所示:

//Iterate through the dynamic Object's list of properties, looking for match from the facebook mapping lookup 
     foreach (var entry in properties) 
     { 
     var MatchedResults = PropertyLookup.Where(x => x.facebookparent == entry.Key || x.facebookfield == entry.Key); 

     if (MatchedResults != null) 
      foreach (propertycontainer DestinationPropertyInfo in MatchedResults) 
      { 
        object mappedValue =null; 
        if (entry.Value.GetType().Name == "JsonObject") 
        { 
         //drill down on level to obtain a list of properties from the child set of properties 
         //of the dynamic Object 
         mappedValue = FindMatchingChildPropertiesRecursively(entry, DestinationPropertyInfo);       

         //child properity was not matched so apply the parent FacebookJson object as the entry value 
         if (mappedValue == null && 
          DestinationPropertyInfo.facebookfield == entry.Key) 
          mappedValue = entry.Value; 

        } 
        else 
        { 
         if (String.IsNullOrEmpty(DestinationPropertyInfo.facebookparent) && 
          DestinationPropertyInfo.facebookfield == entry.Key) 
          mappedValue = entry.Value; 
        } 

        //copy mapped value into destination class property 
        if (mappedValue != null) 
         if (DestinationPropertyInfo.facebookMappedProperty.PropertyType.Name == "DateTime") 
         { 
          DestinationPropertyInfo.facebookMappedProperty.SetValue(entity, System.DateTime.Parse(mappedValue.ToString()), null); 
         } 
         else 
          DestinationPropertyInfo.facebookMappedProperty.SetValue(entity, mappedValue, null); 
      } 
    } 
    return entity; 
} 

在調試模式下,我在我的模型視圖屬性上獲得瞭如下圖所示的空值。 Properties with null Values 並且由facebook返回的Json也不是有效的json以便序列化/反序列化。任何解決方法?請幫助。

編輯: 模型值被映射至Facebook項如下:

[Required] 
     [FacebookMapping("likes")] 
     public dynamic Likes { get; set; } 
     [Required] 
     [FacebookMapping("name")] 
     public string Name { get; set; } 

     [FacebookMapping("pic_large")] 
     public string ImageURL { get; set; } 

EDIT2: 從Facebook GrapAPICall檢索的Json是如下:

{"data":[{"id":"1264038093655465_1274837905908817","likes":{"data":[{"name":"Sayed Zubair Hashimi","pic_large":"https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/14909900_10154513795037597_3241587822245799922_n.jpg?oh=54ead7e0ba74b45b632d96da1515ccf8&oe=591C4938","id":"10154729171332597"} 

回答

0

作爲你注意到你提供的Json是無效的,至少]}}]}錯過了。加入後缺失的元素相關者的物體看起來像如下圖所示:

public class PostDetail 
{ 
    public string name { get; set; } 
    public string pic_large { get; set; } 
    public string id { get; set; } 
} 

public class Likes 
{ 
    public List<PostDetail> data { get; set; } 
} 

public class Post 
{ 
    public string id { get; set; } 
    public Likes likes { get; set; } 
} 

public class RootObject 
{ 
    public List<Post> data { get; set; } 
} 
+0

是否有可能作出一點點的解釋,據我是新來的C#。我怎樣才能實現這個? –

+0

認爲這些列表正在充當包裝從給我看到的附加括號。 – Netferret