MiniJSON knowledge
1) Anything that is wrapped in curly brackets {} converts to "Dictionary<string, object>"
2) all the keys are sthe strings on the left side of the colon (:)
3) values are on the right side of the colon (:)
4) anything wrapped in square brackets [] convert to a List<object>
5) all values converts to System.String which then be converted to whatever type you want with the system string convert functions.
Some examples to serialize / deserialize facebook sdk output in Unity3D:
void OnLoggedIn()
{
//Debug.Log("Logged in. ID: " + FB.UserId);
// Reqest player info and profile picture
FB.API("/me?fields=id,first_name,last_name,picture,friends.limit(10).fields(first_name,id,last_name,picture)", Facebook.HttpMethod.GET, APICallback);
//LoadPicture(GetPictureURL("me", 128, 128),MyPictureCallback);
}
void APICallback(FBResult result)
{
Debug.Log("APICallback");
if (result.Error != null)
{
Debug.LogError(result.Error);
return;
}
myFacebookProfile = DeserializeJSONProfile(result.Text);
// Debug.Log("+++++++++++ facebook profile :" +
// "firname: " + myFacebookProfile.first_name +
// ", lastname:" + myFacebookProfile.last_name +
// ",id: " + myFacebookProfile.id +
// "picture: " + myFacebookProfile.profile_image_url+
// ",is_silhoutte" + myFacebookProfile.Profile_image_is_silhoutte);
myFacebookFriends = DeserializeJSONFriends(result.Text);
int i = 0;
foreach (FacebookProfile fd in myFacebookFriends) {
i++;
Debug.Log("+++++++++++" + i +") nolufriend:" +
"firname: " + fd.first_name +
", lastname:" + fd.last_name +
",id: " + fd.id +
"picture: " + fd.profile_image_url+
",is_silhoutte" + fd.Profile_image_is_silhoutte);
}
}
public FacebookProfile DeserializeJSONProfile(string response)
{
FacebookProfile profile = new FacebookProfile();
Dictionary<string, object> responseObject = Json.Deserialize(response) as Dictionary<string, object>;
object nameH;
Dictionary<string,object> tempDictionary;
if (responseObject.TryGetValue("first_name", out nameH))
{
profile.first_name = (string)nameH;
}
if (responseObject.TryGetValue("last_name", out nameH))
{
profile.last_name = (string)nameH;
}
if (responseObject.TryGetValue ("id", out nameH)) {
profile.id = (string)nameH;
}
if (responseObject.TryGetValue ("picture", out nameH)) {
tempDictionary = (Dictionary<string,object>) nameH;
if (tempDictionary.TryGetValue ("data", out nameH)) {
tempDictionary = (Dictionary<string,object>) nameH;
profile.profile_image_url = (string) tempDictionary["url"];
profile.Profile_image_is_silhoutte = (bool) tempDictionary["is_silhouette"];
//resim bilgisi gönderilmiş ise, resmi download edelim arka planda:
if(profile.Profile_image_is_silhoutte == false) {
StartCoroutine(LoadProfilePictureEnumerator(profile));
}
}
//Dictionary<string,object> friendPictureData = (Dictionary<string,object>)((Dictionary<string,object>)responseObject["picture"])["data"];
//profile.profile_image_url = (string) friendPictureData["url"];
//profile.Profile_image_is_silhoutte = (bool) friendPictureData["is_silhouette"];
}
return profile;
}
public List<FacebookProfile> DeserializeJSONFriends(string response)
{
List<FacebookProfile> facebookFriends = new List<FacebookProfile>();
var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
object friendsH;
Dictionary<string,object> tempDictionary;
bool runned_once = false;
do {
if (!responseObject.TryGetValue("friends", out friendsH))
{
break;
}
List<object> friendsDataContainer = (List<object>)(((Dictionary<string, object>)friendsH)["data"]);
foreach (Dictionary<string,object> friendData in friendsDataContainer) {
FacebookProfile friend = new FacebookProfile();
if (friendData.TryGetValue("first_name", out friendsH))
{
friend.first_name = (string)friendsH;
}
if (friendData.TryGetValue("last_name", out friendsH))
{
friend.last_name = (string)friendsH;
}
if (friendData.TryGetValue ("id", out friendsH)) {
friend.id = (string)friendsH;
}
//resimleri bulmak için bir tur daha aşagı inecegiz
if (friendData.TryGetValue ("picture", out friendsH)) {
tempDictionary = (Dictionary<string,object>) friendsH;
if (tempDictionary.TryGetValue ("data", out friendsH)) {
tempDictionary = (Dictionary<string,object>) friendsH;
friend.profile_image_url = (string) tempDictionary["url"];
friend.Profile_image_is_silhoutte = (bool) tempDictionary["is_silhouette"];
//resim bilgisi gönderilmiş ise, resmi download edelim arka planda:
if(friend.Profile_image_is_silhoutte == false) {
StartCoroutine(LoadProfilePictureEnumerator(friend));
}
}
//Dictionary<string,object> friendPictureData = (Dictionary<string,object>)((Dictionary<string,object>)responseObject["picture"])["data"];
//profile.profile_image_url = (string) friendPictureData["url"];
//profile.Profile_image_is_silhoutte = (bool) friendPictureData["is_silhouette"];
}
//
// //var friendPictureDataContainer = friendData ["picture"] as Dictionary<string,object>;
// //var friendPictureData = friendPictureDataContainer["data"] as Dictionary<string,object>;
// Dictionary<string,object> friendPictureData = (Dictionary<string,object>)((Dictionary<string,object>)friendData["picture"])["data"];
//
// friend.profile_image_url = (string) friendPictureData["url"];
// friend.Profile_image_is_silhoutte = (bool) friendPictureData["is_silhouette"];
//
// //profile resmini arka planda download et ve profile classına yükle
// StartCoroutine(LoadProfilePictureEnumerator(friend));
//friend bilgilerini listeye ekle
facebookFriends.Add(friend);
}
runned_once = true;
} while (!runned_once);
return facebookFriends;
}
No comments:
Post a Comment