2

我正在使用一个名为 prime[31] 社交网络插件的统一插件来处理 Facebook。它适用于这样的事情:

void onGraph()
    {
        Facebook.instance.graphRequest( "me", HTTPVerb.GET, ( error, obj ) =>
        {
            // if we have an error we dont proceed any further
            if( error != null )
                return;             
            if( obj == null )
                return;             
            // grab the userId and persist it for later use
            var ht = obj as Dictionary<string,object>;
            _userId = ht["id"].ToString();      

            Debug.Log( "me Graph Request finished: " + _userId );
            Prime31.Utils.logObject( ht );
        } );
    }

它返回这个:

 --------- IDictionary ---------
id: ########## 
name: Mike O'Connor 
first_name: Mike 
last_name: O'Connor 
link: https://www.facebook.com/########## 
username: ########## 
etc...

而且我似乎可以抓住任何键并将其填充到这样的变量中,对吗?:

_userId = ht["id"].ToString();

但是当我要求每个人玩我的游戏的分数时:?

void onGetScore()

    {
        Facebook.instance.graphRequest( "AppID/scores", HTTPVerb.GET, ( error, obj ) =>
        {
            // if we have an error we dont proceed any further
            if( error != null )
                return;

            if( obj == null )
                return;
            // grab the userId and persist it for later use
            var ht = obj as Dictionary<string, object>;
            _score = ht["score"].ToString();
            Prime31.Utils.logObject( ht );
        } );
    }

它返回一个嵌套字典/列表,如下所示:

 --------- IDictionary --------- 

 --------- IList --------- 
data: 
    --------- IDictionary ---------
    user:    --------- IDictionary ---------
        name:   Mike O'Connor
        id:     ##########
        score:  5677
        application:     --------- IDictionary ---------
            name:   ##########
            namespace:  ##########
            id:     ##########

现在它是嵌套的,所以我不能使用它,对吧?

_score = ht["score"].ToString();

如何获取嵌套键?这只是语法问题还是我必须重铸(或其他)?

4

2 回答 2

1

So you want to access the value of a key-value pair inside a dictionary, inside a dictionary, inside a list, inside a dictionary?

It sounds like you want this:

_score = ht["data"][0]["score"].ToString();

Note, the 0 here represents the first item in the ht["data"] list.

Unfortunately it looks like your objects are probably weakly typed. In that case you'd have to do:

_score = 
    ((IDictionary<string, object>)
        ((IList<object>)ht["data"])[0])["score"].ToString();

Which looks terrible, but should work (assume the types returned are really what you describe).

To get an set of user name / score pairs, you can use something like this Linq query:

var scores = 
    from item in ((IList<object>)ht["data"]).Cast<IDictionary<string, object>>()
    let name = ((IDictionary<string, object>)item["user"])["name"].ToString()
    let score = item["score"].ToString()
    select new { username = name, score };
于 2013-08-02T18:30:53.193 回答
0

您将需要更改属性访问逻辑。ht["score"]不再存在(或者至少不在外部级别,它在字典中的字典中的列表中)。相反,它会像ht["list1"].First()["score"].ToString()但是我不确定确切的路径是什么,因为您的数据格式不清楚。你能用原始响应更新吗?那我可以肯定的告诉你。另外,我认为您可能需要进行一些迭代。因为该结构多次嵌套以获取内部值,所以您必须迭代外部字典,然后迭代它包含的列表,然后访问列表包含的字典中的值。

于 2013-08-02T18:29:52.993 回答