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

Asked

Viewed 70 times

1

I’m trying to use the goo.Gl API to shorten some links. The problem is I don’t know how to get the generated return json URL. 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 draw just the id of json in my controller using C#. I’m using the MVC concept in ASP.NET.

2 answers

1

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

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

Install-Package Google.Apis.Urlshortener.v1

After that, just use the API this way:

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;

0


Well, just give one 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);
  • made similar, but with Jsonobject. very good this library

Browser other questions tagged

You are not signed in. Login or sign up in order to post.