反序列化Facebook照片结果

时间:2015-06-23 01:31:32

标签: c# json unity3d facebook-unity-sdk

我试图反序列化从FB请求返回的结果。 我设法通过Facebook使用以下两行来获取已经通过Facebook登录我的应用程序的用户的照片列表:

string meQueryString = "/me?fields=photos";
FB.API(meQueryString, Facebook.HttpMethod.GET,GetPhotosCallback );

抱着我的唯一问题是我无法弄清楚如何反序列化FBResult。

这是我的代码:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Facebook.MiniJSON;
using UnityEngine.UI;
using Facebook;
public class User_Photos : MonoBehaviour {

private static List<object>  PhotosData ;
//public string link;
 void Awake()
{
    FB.Init(SetInit, onHideUnity);
}

private void SetInit()
{
    Debug.Log("FB Init Done");
    if (FB.IsLoggedIn) {
        Debug.Log ("FB lOGGED iN");
    }else {
        //FBLogin();
    }
}

private void onHideUnity(bool isGameShown)
{
    if (!isGameShown) {
        Time.timeScale = 0;
    } else 
    {
        Time.timeScale = 1;
    }
}

string meQueryString = "/me?fields=photos";
public void FacebookLogin()
{
    Debug.Log("Facebook Login Part");
    FB.Login ("user_photos, user_about_me", LoginCallback);
}
void LoginCallback(FBResult result)
{
    if (FB.IsLoggedIn) {
        Debug.Log ("user Logged in, Know We try To get Photos ");
        GetPhotos();
    } else {
        Debug.Log(" user failed");
    }
}

void GetPhotos()
{
    //if (FB.IsLoggedIn) {
        Debug.Log("GetPhotos");
    FB.API(meQueryString, Facebook.HttpMethod.GET,GetPhotosCallback );
    //}
}

void GetPhotosCallback(FBResult GetPhotosCallbackResult)
{
    PhotosData = DeserializeJasonPhotos (GetPhotosCallbackResult.Text);

    Debug.Log ("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh" + PhotosData);
    foreach (Text im in PhotosData) {
        Debug.Log (im);
    }

}

public static List<object> DeserializeJasonPhotos(string jsonString)
    {
    Debug.Log ("ghhhhhhhhhhhhhhhhhhhhhhggggg");

    var reponseobject = Json.Deserialize (jsonString) as Dictionary<string, object>;
    object PicturesH;
    var photos = new List<object>();
    if (reponseobject.TryGetValue ("data", out PicturesH)) {
        Debug.Log (" try get value");
        photos = (List<object>)(((Dictionary<string, object>)PicturesH) ["images"]);

        if (photos.Count > 0) {
            var photoDict = ((Dictionary<string, object>)(photos [1]));
            var photo = new Dictionary<string,string> ();
            photo ["source"] = (string)photoDict ["source"];

            Debug.Log (photoDict);



        }
        //foreach (string link in Photos) {
        //print( photo["source"]);
    } else {
          Debug.Log("faillllllllllllllllllllldddddddddddddddddddd");
     }

    return photos;
    //Debug.Log ("photossss");
}

}

这是FBresult

{
 "photos": {
   "data": [
    {
    "id": "769988419745989", 
    "created_time": "2015-02-23T17:07:07+0000", 
    "from": {
      "id": "806977726047058", 
      "name": "Raed Lafi"
    }, 
    "height": 720, 
    "icon": "https://static.xx.fbcdn.net/rsrc.php/v2/yz/r/StEh3RhPvjk.gif", 
    "images": [
      {
        "height": 720, 
        "source": "https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/11017839_769988419745989_5648743920431828327_n.jpg?oh=757cc110e49e9861cac40e127e4752c7&oe=55ED32B1", 
        "width": 720
      }, 
      {
        "height": 600, 
        "source": "https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/p600x600/11017839_769988419745989_5648743920431828327_n.jpg?oh=ece74dd3c1799a4acc50ca89f6deb8d3&oe=55F2D4AF", 
        "width": 600
      }, 
      {
        "height": 480, 
        "source": "https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/p480x480/11017839_769988419745989_5648743920431828327_n.jpg?oh=98daf2a09e20217d59eec14671aa7cce&oe=56204A4F", 
        "width": 480
      }, 
      {
        "height": 320, 
        "source": "https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/p320x320/11017839_769988419745989_5648743920431828327_n.jpg?oh=4f61569d5c2ee246e5e031cea1f8f739&oe=5634EE15", 
        "width": 320
      }, 
      {
        "height": 540, 
        "source": "https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/p180x540/11017839_769988419745989_5648743920431828327_n.jpg?oh=31d93f7b3acd4b03275c5b074cca49e5&oe=562F1CA8", 
        "width": 540
      }, 
      {
        "height": 130, 
        "source": "https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/p130x130/11017839_769988419745989_5648743920431828327_n.jpg?oh=3539be4afc60f75d50a02045c8fd7993&oe=55F1DC7A", 
        "width": 130
      }, 
      {
        "height": 225, 
        "source": "https://scontent.xx.fbcdn.net/hphotos-xpf1/v/t1.0-9/p75x225/11017839_769988419745989_5648743920431828327_n.jpg?oh=25f061ef2c9dd1cbf1c0db07d9233d12&oe=56236BF9", 
        "width": 225
      }
    ], 

0 个答案:

没有答案