How to fetch a json value from the goo.gl API for C #

1

I'm trying to use the goo.gl API to shorten some link. The problem is that I do not know how to pull the generated URL from the json return. See below how it is organized:

{
 "kind": "urlshortener#url",
 "id": "AQUI VEM A URL DO GOOGLE",
 "longUrl": "http://url-dos-detalhes/"
}

And I need to get only the id of json in my controller using C #. I'm using the MVC concept in ASP.NET.

    
asked by anonymous 23.02.2017 / 14:00

2 answers

0

Well, just give deserialize using a type dynamic :

 using Newtonsoft.Json;

 var output = JsonConvert.DeserializeObject<dynamic>("{ \"kind\": \"urlshortener#url\", \"id\": \"AQUI VEM A URL DO GOOGLE\", \"longUrl\": \"http://url-dos-detalhes/\"}");
 Console.WriteLine(output.id);
    
23.02.2017 / 17:37
1

Google has the Google.Apis.Urlshortener package. Just install the same via NuGet and use it.

To install, type the following command in the Package Manager Console :

  

Install-Package Google.Apis.Urlshortener.v1

After that, just use the API like this:

UrlshortenerService service = new UrlshortenerService(new BaseClientService.Initializer
{
      ApiKey = "SUA API KEY AQUI"
});

var url = "www.google.com";
Url response = service.Url.Insert(new Url { LongUrl = url }).Execute();

var id = response.Id;
    
23.02.2017 / 14:49