1

I'm trying to parse json data from the facebook c# sdk. The json data I'm trying to parse can be seen here at facebook: https://graph.facebook.com/search?q=coffee&type=place&center=37.76,-122.427&distance=1000&access_token=AAAAAAITEghMBACQupPhpGCGi1Jce7eMfZCzt9GlpZBdhz3PlGCyHKNZB1r4FHgd9mgpm8W3g4Adpy9jJjFrsDuxcu3pE4uRT1lbIQjYKgZDZD

My code below will pop up a message box showing the first dimension of this json object, however, as you can see, there is a second dimension within each item which gives location information such as longitude and latitude. I'm struggling to find an example as to how one would get this with WP7 C# (most examples on the internet use libraries that aren't usable on WP7).

        fbClient.GetCompleted += (o, er) =>
        {
           if (er.Error == null)
           {
              var result = (IDictionary<string, object>)er.GetResultData();
              Dispatcher.BeginInvoke(() =>
              {
                  foreach (var item in (JsonArray)result["data"])
                  {
                     //message box for testing purposes
                     MessageBox.Show((string)((JsonObject)item)["name"]);
                  }
              });
           }
        });

Would someone be able to provide a quick example?

Thanks.

4
  • look at stackoverflow.com/questions/10741374/json-net-json-to-entity it might give you a clue Commented May 27, 2012 at 7:56
  • Do you know which assembly's I would have to reference? This seems like a better approach. Commented May 27, 2012 at 8:28
  • Do you know which assembly's I would have to reference? I can't seem to get VS2010 to recognize JsonConvert.DeserializeObject for WP7, however, this link you've posted seems like a better approach. Commented May 27, 2012 at 8:41
  • I think that sample uses Json.Net Commented May 29, 2012 at 21:44

1 Answer 1

1

Because you use with FacebookSDK it's not neccecary work with json directly. Just cast JsonObjects to IDictionary and work with it like Dictionary:

//think better use IEnumerable<object>, because you don't really need JSON array
    foreach (var item in (IEnumerable<object>)result["data"])
                      {
                         var name = (item as IDictionary<string, object>)["name"];
                         //message box for testing purposes
                         MessageBox.Show(name);
                      }

So, you can work with JsonArray like IEnumerable<object> and with JsonObject like IDictionary<string, object>

Answer for you question:

var item1 = (IDictionary<string, object>)item;
var location = ((IDictionary<string, object>)(item1)["location"]);
var long = location["longitude"];

Or you can do it using JSON:

var location = ((JsonObject)((JsonObject)item)["location"]);
var long = location["longitude"];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.