C# SDK

2012-09-26 2 views
0

을 사용하여 Windows Phone 7의 Facebook 페이지 가져 오기 Facebook C# SDK 페이지의 Windows Phone 샘플을 사용하여이 문제를 파악하려고했지만 실패했습니다.C# SDK

private void GetPages() 
    { 
     var fb = new FacebookClient(_accessToken); 

     fb.GetCompleted += (o, e) => 
     { 
      if (e.Error != null) 
      { 
       Dispatcher.BeginInvoke(() => MessageBox.Show(e.Error.Message)); 
       return; 
      } 

      var result = (IDictionary<string, object>)e.GetResultData(); 
      // returns data and paging from Facebook 

      Dispatcher.BeginInvoke(() => 
      { 
       foreach (var item in result) 
       { 
        // Not sure if/how to use the custom classes here 
        //item has .Key and .Value 
        //.Key = data and .Value contains the key/value pais for each of the pages returned 
       } 

      }); 
     }; 

     fb.GetAsync("me/accounts"); 
    } 

// 사용자 정의 클래스

public class FacebookPageCollection 
    { 
     [JsonProperty(PropertyName = "data")] 
     public FacebookPage[] data { get; set; } 
     [JsonProperty(PropertyName = "paging")] 
     public FacebookPagePaging paging { get; set; } 
    } 

    public class FacebookPage 
    { 
     [JsonProperty(PropertyName = "name")] 
     public string Name { get; set; } 

     [JsonProperty(PropertyName = "access_token")] 
     public string AccessToken { get; set; } 

     [JsonProperty(PropertyName = "category")] 
     public string Category { get; set; } 

     [JsonProperty(PropertyName = "id")] 
     public string Id { get; set; } 
    } 

    public class FacebookPagePaging 
    { 
     [JsonProperty(PropertyName = "previous")] 
     public Uri previous { get; set; } 

     [JsonProperty(PropertyName = "next")] 
     public Uri next { get; set; } 
    } 

이 변수 "결과"를 반환 것입니다 :

다음은 주요 코드의 { "데이터": [{ "이름": " 값 1 ","access_token ":"값 2 ","범주 ":"값 3 ","id ":"값 4 ","perms ": ["ADMINISTER ","EDIT_PROFILE ","CREATE_CONTENT ","MODERATE_CONTENT ","CREATE_ADS ","BASIC_ADMIN "]}, {"name ":"value1 ","access_token ":"value2 ","category ":"value3 ","id ":"value4 ","perms ": ["ADMINISTER " "EDIT_PROFILE", "CREATE_CONTENT", "MODERATE_CONTENT", " "" "다음": "url"}}

각 페이지에 대한 세부 정보를 검색하고 저장하는 것이 좋습니다.

나는 이것을 알아 내려고 노력해 왔으며 여기서 다른 곳의 여러 게시물을 살펴 보았습니다. 나는 그걸 알아낼 수있는 충분한 경험이 없습니다.

도움을 주시면 감사하겠습니다.

감사합니다. 스리

+0

내 응용 프로그램에서 CSharpSDK를 사용하고 싶은만큼 이전 코드를 고수했습니다. 나는 이것을 언젠가 알아낼 수 있을지 모른다. – Sri

답변

1

다음은 fb C# sdk에서 json 응답을 사용하는 방법을 이해하는 트릭입니다.

다음은 Javascript JSON과 C# JSON 간의 매핑입니다.

다음
JsonObject => keyvalue pairs => IDictionary<string, object>/IDictinary<string, dynamic> 
JsonArray => array => IList<object>/IList<dynamic> 
string => string 
number => long/decimal 
boolean => bool 

당신이 실제 매핑을 수행하는 방법이다 (이 JSON.org에서 발견 된 JSON 사양의 일부가 아니므로주의에는 날짜 시간 및 다른 복잡한 .NET 객체가 없다).

var result = (IDictionary<string, object>)e.GetResultData(); 
var data = (IList<object>)result["data"]; 
foreach(var act in data) { 
    var account = (IDictionary<string,object>) act; 
    var name = (string)account["name"]; 
    var accessToken = (string)account["access_token"]; 
    var id = (string)account["id"]; 

    // normalize to IList<string> permissions, so it is easier to work without casting. 
    var permissions = ((IList<object>)account["perms"]).Select(x => (string)x).ToList(); 
}