Search code examples
c#asp.net-corefacebook-graph-apijson.netrazor-pages

Loop through a json response received from facebook graph api in c# and get the latest value. (then, store it in database in dot net core)


I'm using the graph API of facebook to get the permalink of facebook posts. But it seems the endpoint always returns an array of all the posted posts.

So, I need to take the response and loop through it and get only the latest value that is always the first pair in the response.

Here is the response I get using the graph explorer. I only need the first pair from data array i.e the permalink_url and the id so that I can store them separately in a database to embed posts in my website using the permalink.

{
  "data": [
    {
      "permalink_url": "https://www.facebook.com/347775946439542/photos/a.348021906414946/358023508748119/?type=3",
      "id": "347775946439542_358023508748119"
    },
    {
      "permalink_url": "https://www.facebook.com/347775946439542/photos/a.348021906414946/350654269485043/?type=3",
      "id": "347775946439542_350654269485043"
    },
    {
      "permalink_url": "https://www.facebook.com/347775946439542/photos/a.348021906414946/350651839485286/?type=3",
      "id": "347775946439542_350651839485286"
    }
  ],
  "paging": {
    "cursors": {
      "before": "QVFIUlRGODNTaFZAueEUxQ2VvcHlrMUw1MGE0U0FCblhZAY1hVbFBGUHhHdXlrSkgzSm0tb05pSGpFOXBYVG9EcnN5T21sQTgtYy1jSjhrZAW9WYmg5YmpVMXpQWTRLUkQ3ZA05PcEZAjUTNyV2VuV05hRG8yVlBaa3pia3dFTVRLU05nU0pU",
      "after": "QVFIUlBHMG44ZAkxHdUsxb012bVlOQ0ZAfaDhHLUZA6VEt6MHd0QjJYZADlfZAVk2aExSdlJESUU2SlVDaVJsYzI4dnlmUllZASzhKd3Nkb09aa2ZAMRHZAFTGhMVHFoQ1hjNE5zNDJ2aV96UGtVQUpBeHE1b2xUTzk2SHZALLTZA1Y3pZAZAmJOMENS"
    }
  }
}

I did go through some questions on this that used newtonsoft but couldn't get it to work. So any help on how to loop through this in C# (razor page) and then return them from a method. Thenafter I need to store both the permalink as well as the id in database.

Here is my method: Here _getPermalink has the endpoint I have to hit.

Also 'str' has the response which is shown below. I have to fetch the permalink and id from that.

public string GetPermalink()
        {

            WebClient client = new WebClient();
            Stream data = client.OpenRead(_getPermalink);
            StreamReader reader = new StreamReader(data);
            string str = "";
            str = reader.ReadLine();
            data.Close();

            return str;
        }

When I call this endpoint from the method:string res = facebook.GetPermalink(); (Here facebook is just a constructor with values required for making the call to endpoint such as page id, access token,etc.)

The same response looks something like this.

{"data":[{"permalink_url":"https:\/\/www.facebook.com\/347775946439542\/photos\/a.348021906414946\/358023508748119\/?type=3","id":"347775946439542_358023508748119"},{"permalink_url":"https:\/\/www.facebook.com\/347775946439542\/photos\/a.348021906414946\/350654269485043\/?type=3","id":"347775946439542_350654269485043"},{"permalink_url":"https:\/\/www.facebook.com\/347775946439542\/photos\/a.348021906414946\/350651839485286\/?type=3","id":"347775946439542_350651839485286"}],"paging":{"cursors":{"before":"QVFIUlRGODNTaFZAueEUxQ2VvcHlrMUw1MGE0U0FCblhZAY1hVbFBGUHhHdXlrSkgzSm0tb05pSGpFOXBYVG9EcnN5T21sQTgtYy1jSjhrZAW9WYmg5YmpVMXpQWTRLUkQ3ZA05PcEZAjUTNyV2VuV05hRG8yVlBaa3pia3dFTVRLU05nU0pU

I'm relatively new with the concept of json object/arrays in C# and using newtonsoft. Any help and explanations are appreciated. (PS: Is there a way to get the permalink and id of the Latest Posts ONLY, using the facebook graph api?)


Solution

  • Given the JSON data you posted, you can get the first permalink URL from it using the following one-liner:

    string permalinkUrl = (string)JObject.Parse(json).SelectToken("data[0].permalink_url");
    

    Demo: https://dotnetfiddle.net/SUgz51