Search code examples
c#asp.net-mvcasp.net-corebitly

How to access short URl from Json object in c# - Bitly


I am using Bitly api in Asp.net project to create short Url. I am getting result but it is in json format. How do I retrieve short Url from it?

[HttpGet("sendNotification/{PlanId}")]
        [ValidateModelState]
        public async Task<IActionResult> SendNotification()
        {
                var link ="https://chats.landbot.io";
                var shortUrl = await GetShortUrl(link);
              
                return NoContent();
            }
            
        }

shortUrl giving me everything as below

{"created_at":"2021-10-21T04:01:53+0000","id":"bit.ly/shortUrl","link":"LongUrl","archived":false,"tags":[],"deeplinks":[],"references":{"group":"https://api-ssl.bitly.com/v4"}}

In the above line, id contains the Short link and that's the only link I need. Can anyone tell me what I should do?


Solution

  • 1.JObject:

    {{"created_at":"2021-10-21T04:01:53+0000","id":"bit.ly/shortUrl","link":"LongUrl","archived":false,"tags":[],"deeplinks":[],"references":{"group":"https://api-ssl.bitly.com/v4"}}}
    

    If you want to get idfrom JObject,you can use:

    var id=jObject["id"].Tostring;
    

    2.json string:

    {\"created_at\":\"2021-10-21T04:01:53+0000\",\"id\":\"bit.ly/shortUrl\",\"link\":\"LongUrl\",\"archived\":false,\"tags\":[],\"deeplinks\":[],\"references\":{\"group\":\"https://api-ssl.bitly.com/v4\"}}
    

    If you want to get id from json string,you can use:

    var id=JObject.Parse(jsonString)["id"].Tostring;