1

I'm getting the following Json format string as a result from a Facebook graph search request :

{
  "data": [
    {
      "name": "Event A", 
      "start_time": "2013-11-08T19:00:00+0200", 
      "end_time": "2013-11-10T00:00:00+0200", 
      "timezone": "Europe/Bucharest", 
      "id": "232252355126"
    }, 
    {
      "name": "Event B", 
      "start_time": "2013-11-08T13:00:00+0200", 
      "end_time": "2013-11-09T16:00:00+0200", 
      "timezone": "Europe/Bucharest", 
      "location": "Bucharest", 
      "id": "414334343426"
    }, 
    {
      "name": "Event C", 
      "start_time": "2013-10-30T18:30:00+0200", 
      "timezone": "Europe/Bucharest", 
      "location": "Bucharest", 
      "id": "44315995273"
    }
  ], 
  "paging": {
    "previous": "https://graph.facebook.com/search?limit=3&type=event&q=Bucharest&since=1383930000&__paging_token=22251255126", 
    "next": "https://graph.facebook.com/search?limit=3&type=event&q=Bucharest&until=1383150600&__paging_token=44115995273"
  }
}

I'm encountering some errors while trying to retrieve data from this JSON. I've tried with

dynamic jsonData = await facebookClient.GetTaskAsync(string.Format("https://graph.facebook.com/search?q={0}&type=event&limit={1}&offset={2}", locationKeyword, limit, offset));
dynamic result = JsonConvert.DeserializeObject(jsonData.ToString());

Some answers direct me to use JavaScriptSerializer but I don't have the namespace for that class, as I'm using API for developing Windows 8 Apps.

I can't manage how to get the events as somehow from data object.

I tried accessing the values in the immediate windows in VS as result.data but it's not working.

I search on how to make this but most answers seem to say to create a class in which the json data will fit.

Can't I achieve this with dynamic? (something like result.data.name, result.paging.previous etc)

RealityDysfunction
  • 2,609
  • 3
  • 26
  • 53
VasileF
  • 2,856
  • 2
  • 23
  • 36
  • 1
    What errors? tested with this json and got results from using result.data[0]. – Marvin Smit Nov 11 '13 at 21:53
  • 1
    Are you making sure to handle the `data` as an array? – DROP TABLE users Nov 11 '13 at 21:53
  • @MarvinSmit I tried in the immediate window, with result.data[0] and I'm getting : 'object' does not contain a definition for 'data' and no extension method 'data' accepting a first argument of type 'object' could be found. So data doesn't exist in result. – VasileF Nov 11 '13 at 21:56
  • what is the variable jsonData? what type? what is it's actual contents? are you sure it's the json you expect it to be? – Marvin Smit Nov 11 '13 at 21:59
  • My goal is to put the fields name, start_time, end_time etc in a IEnumerable so I could iterate over them later, when I'll need to. – VasileF Nov 11 '13 at 22:00
  • @MarvinSmit jsonData is of type dynamic. DeserializeObject method, requires a string type parameter. So I made a cast to string. At debug, everything seems fine, I can see the json as a string. – VasileF Nov 11 '13 at 22:00
  • @RealityDysfunction There's no ASP.NET here. I am making a request to the Facebook server with a query, and it returns me the data which is stored in a variable of type dynamic (jsonData). Afterwards, I try to parse it and I get another dynamic (result), which seems just a big string and from result, I can't manage to figure it out how to access the values. – VasileF Nov 11 '13 at 22:11
  • JSON.Stringify() is a JavaScript function. – RealityDysfunction Nov 11 '13 at 22:13
  • This looks to be very similar to what you are trying to do, maybe it will help. http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – DROP TABLE users Nov 11 '13 at 22:24
  • @DROPtableusers, thank you for the reply, but I looked over the implementation, and I don't have access to the System.Web namespace in a Windows Store app, it is not available. – VasileF Nov 12 '13 at 09:24
  • @VasileMarianFălămaș sorry about that, could you possibly add a reference to some third party json library? I have used and recommend this one: http://json.codeplex.com/ – DROP TABLE users Nov 12 '13 at 17:08
  • 1
    Thanks, @DROPtableusers, yes, I've found Json from Newtonsoft, and I posted my solution. There is also the built-in Json in Windows.Data.Json namespace, but it failed to parse this. It could be I didn't manage through it but in any case, it has few methods... – VasileF Nov 12 '13 at 20:18

2 Answers2

0

I have done this exact thing before, except I converted into an XML, My Example:

(1 - JavaScript) var finalStr = JSON.stringify(facebookString)
(2 - ASP.NET) JsonConvert.DeserializeXmlNode("{\"root\":" + received_json + "}","root");
RealityDysfunction
  • 2,609
  • 3
  • 26
  • 53
  • I don't see how this could solve my issue, it gets the data converted from Json to XML, but my issue remains, I still can't manage to access those members. – VasileF Nov 12 '13 at 09:17
  • You need to install a NuGet package to get JsonConvert in your Windows project. It is made by NewtonSoft. – RealityDysfunction Nov 12 '13 at 14:59
  • I did that of course, but the problem was actually accessing the object members. I managed though without XML, with Json via JObject and JArray, I will post the answer later today. Thank you for help. – VasileF Nov 12 '13 at 15:54
0

I managed at last to access the members...

To access name for example, or start_time, I did the following :

dynamic jsonData = await facebookClient.GetTaskAsync(string.Format("https://graph.facebook.com/search?q={0}&type=event&limit={1}&offset={2}", locationKeyword, limit, offset));

var result = JObject.Parse(jsonData.ToString());
var array = new JArray(result["data"]);

var a = array[0];

string name = (string) a.SelectToken("name");
var date = (DateTime?) a.SelectToken("start_time");

There might be better implementations but this one worked in my case. I posted it, in case it might be of help to others seeing this post.

Best wishes.

VasileF
  • 2,856
  • 2
  • 23
  • 36