- Home /
 
Get values from long string (C#)
Hi I am currently integrating the Facebook Unity SDK in my game and when I load an app request, I get this result from facebook:
{ "id": "453305136468701", "application": { "name": "TEST_APP", "namespace": "test", "id": "1409535269881375" }, "to": { "name": "Recipient Name", "id": "100000195788968" }, "from": { "name": "Sender Name", "id": "100001527898169" }, "message": "TEST_APP is amazing! Check it out.", "created_time": "2014-03-29T16:32:33+0000" }
My question is now, how can I get for example the value of "id" from the field "from"?
Thank you in advance
Answer by trololo · Mar 30, 2014 at 10:06 PM
This is a JSON format file. Just use a json parsor (JSONObject in C# :)
You will be able to do something like jsonObj.GetField("name").str or jsonObj.GetField("number").n
Could you please give an example of how I can get from the "from" field the "id" value? ? I don't understand how Json works :(
You could use this external class: http://wiki.unity3d.com/index.php?title=JSONObject then you do
 JSONObject j = new JSONObject(theExampleStringYouGaveUs);
 string id = j.GetField("from").GetField("id").str;
 
                 Answer by Scribe · Mar 30, 2014 at 10:07 PM
For this you can use the string.Split method which returns a string array splitting the string at the character(s) you define. For example:
 public string str;
     
 void Start () {
     string[] split = str.Split('"');
     for(int strIndex = 0; strIndex < split.Length; strIndex++){
         Debug.Log(split[strIndex] + " at index " + strIndex);
     }
         
     Debug.Log("For example the id is: " + split[3]);
 }
 
               If you give this your sptring, it will return all the split strings and the last debug will return your id number.
Scribe
Answer by gfoot · Mar 31, 2014 at 08:22 AM
That data is in JSON format, so the easiest way to read it is using a JSON parser like http://wiki.unity3d.com/index.php/SimpleJSON
Answer by worldofcars · Mar 31, 2014 at 03:50 PM
Could somebody please give an example of how I can get from the "from" field the "id" value? ? I don't understand how Json works :(
Answer by paramama · May 10, 2014 at 11:40 AM
For example, you want to load id friends, who played in you games
private void QueryScores() { FB.API("/app/scores?fields=score,user.limit(20)", Facebook.HttpMethod.GET, ScoresCallback); }
void ScoresCallback(FBResult result) {
     scores = new List<object>();
     List<object> scoresList = Util.DeserializeScores(result.Text);
     foreach(object score in scoresList) 
     {
         var entry = (Dictionary<string,object>) score;
         var user = (Dictionary<string,object>) entry["user"];
         string userId = (string)user["id"];
                    //add id to Dictionary
             } 
 
               }
Don't forget to login in FB (FB.Login("email,publish_actions,user_games_activity", LoginCallback);) and used using Facebook.MiniJSON;
Your answer