Problem Deserializing iOS NSObject Facebook Graph Request Response

TomasWinstonTomasWinston ✭✭IEMember ✭✭

Hi,

I am having some difficulty deserializing a response from a Facebook Graph Request (using Xamarin's iOS Facebook SDK).

Using JSON.Net, I cannot deserialize using JsonConvert.DeserializeObject takes a string type. I don't want to do a ".ToString()" because it seems to throw in "=" in place of ":" which causes its own problems. I looked at:

NSJsonSerialization.Deserialize

but that takes an NSData data type.

Here is my code as it stands:

TaskCompletionSource<FacebookProfileResponse> _tcs = new TaskCompletionSource<FacebookProfileResponse>();

if (FBCK.AccessToken.CurrentAccessToken != null)
{
    FBCK.GraphRequest request = new FBCK.GraphRequest("/me", new NSDictionary("fields","name,picture,email"));
    var requestConnection = new FBCK.GraphRequestConnection();
    requestConnection.AddRequest(request, ((connection, res, error) =>
    {
        if (error != null)
        {
            _tcs.SetException(new Exception("Error : " + error.ToString()));
        }
        var profile = JsonConvert.DeserializeObject<FacebookProfileResponse>(res);
        _tcs.SetResult(profile);

    }));
    requestConnection.Start();
}
else {
    _tcs.SetResult(new FacebookProfileResponse());
}

This is the type that I am trying to deserialize to:

public class FacebookProfileResponse
{
    public string id { get; set; }
    public string name { get; set; }
    public FacebookPicture picture { get; set; }
    public string Email { get; set; }
}

public class FacebookPicture
{
    public FacebookPictureData data { get; set; }
}

public class FacebookPictureData
{
    public string url { get; set; }
}

I expect/hope that someone has been in a similar situation and can help out here..

Thanks

T

Answers

  • TylerCitrinTylerCitrin USMember

    Hey @TomasWinston I am currently working on this now and was curious if you had any luck?

  • TomasWinstonTomasWinston ✭✭ IEMember ✭✭

    Hi Tyler,

    No, at the time I didn't have any luck. As far as I can remember (I don't currently have access to that project code) I ended up making a call to the facebook graph api using a HttpClient instead which I think worked for me. Sorry.

    T

  • Patrick.OConnorPatrick.OConnor ✭✭ USMember ✭✭

    Hi,

    result is a NSDictionary so,

    var dict = result as NSDictionary;
    
    var email = dict.ValueForKey(new NSString("email")).ToString();
    var firstname = dict.ValueForKey(new NSString("first_name")).ToString();
    var lastname = dict.ValueForKey(new NSString("last_name")).ToString();
    var picture = dict.ValueForKey(new NSString("picture")).ValueForKey(new NSString("data")).ValueForKey(new NSString("url")).ToString();
    
Sign In or Register to comment.