简体   繁体   English

Facebook取得好友JSON回应无效

[英]Facebook get friends JSON response is invalid

I am developing an app for iOS using Xamarin Studio (C#) in Mac OS X. I want to get a list of the user's friends on Facebook, so I added Facebook's component from Xamarin's component store and made a request(code at the bottom). 我正在Mac OS X中使用Xamarin Studio(C#)开发适用于iOS的应用程序。我想在Facebook上获取用户朋友的列表,因此我从Xamarin的组件存储中添加了Facebook的组件,并提出了要求(底部代码) 。 This is the response I get: 这是我得到的答复:

{
    data =     (
                {
            "first_name" = Dev;
            id = 100001438778777;
            "last_name" = Accu;
        }
    );
    paging =     {
        next = "https://graph.facebook.com/v2.0/100005203000814/friends?fields=id,first_name,last_name&format=json&access_token=CAAK3hGOIm2ABANPUcr2QU1t8gqLNsZCJBrc8ZCZCqUSwHkX2f43VHarvc1ZABbjDrY7jIO0OT5ZBRBiZC1audQnIvxCsOu60y30iR84jVa56beNTptixj7AFqT92ZBGdyxDshFHHxkFDgCg9JyRZBYfqaGKkeJkuxJVUXDq8eR8ZCmRlslpOVSavQZC1hCcxOwdgFS2jWQdGZBFVSYTkrhkavfP&limit=5000&offset=5000&__after_id=enc_Aey-LjP59ZnmKMcZKcEr94tTUPIHIvWj9JnMwkIVSvxJ9RBYBqhBt3bGKlURY4SHBCDeH8BM_wSsqICzEFgKiZvh";
    };
}

There is 2 problems with this response, it only includes 1 friend for some unknown reason, and the JSON is not valid, so ultimately parsing this fails. 此响应有2个问题,由于某种未知原因,它仅包含1个朋友,并且JSON无效,因此最终解析失败。 The following is the code I use to make the request: 以下是我用来发出请求的代码:

    var friendsRequest = await new FBRequest(FBSession.ActiveSession, "/me/friends?fields=id,first_name,last_name").StartAsync();
    var friendsArray = friendsRequest.Result as MonoTouch.Foundation.NSMutableDictionary;
    var response = FriendResponse.FromJson(friendsArray.ToString());

    List<FacebookProfile> friends = new List<FacebookProfile>();
    foreach (var friend in response.Data)
    {
        friends.Add(new FacebookProfile(friend.ID, friend.FirstName, friend.LastName));
    }

And here is the parsing classes: 这是解析类:

public class NextPage
{
    public string Next { get; set; }
}

public class Friend
{
    public string ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class FriendResponse
{
    public List<Friend> Data { get; set; }
    public NextPage Paging { get; set; }

    public static FriendResponse FromJson(string json)
    {
        JsConfig.EmitLowercaseUnderscoreNames = true;
        return JsonSerializer.DeserializeFromString<FriendResponse>(json);
    }
}

The component is a 1:1 binding to the objective-c SDK. 该组件是对Objective-C SDK的1:1绑定。 The response that Facebook returns is an NSDictionary underneath, and if you run ToString() on it, it will return the string representation of the NSDictionary which is totally different from a JSON string. Facebook返回的响应在下面是一个NSDictionary,如果在其上运行ToString(),它将返回NSDictionary的字符串表示形式,该字符串表示形式与JSON字符串完全不同。 What you have to do is serialize that NSDictionary object back into a JSON string. 您要做的是将该NSDictionary对象序列化回JSON字符串。 Here is an example of how to do this: 这是如何执行此操作的示例:

var friendsRequest = await new FBRequest(FBSession.ActiveSession, "/me/friends?fields=id,first_name,last_name").StartAsync();

// Convert back the object into a Json
NSError error;
var jsonData = NSJsonSerialization.Serialize (friendsRequest.Result, 0, out error);
var jsonString = (string) NSString.FromData (jsonData, NSStringEncoding.UTF8);
var response = FriendResponse.FromJson(jsonString);

"jsonString" will have the correct JSON string data representation this time. 这次,“ jsonString”将具有正确的JSON字符串数据表示形式。

Courtesy of Alex DeSoto. 由Alex DeSoto提供。 =) =)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM