# JSON API calls with C#



## mcicognani

Don't know if this is the right place to post, but didn't find a specific thread for developers.

I'm experimenting with WordReference API (thank you guys) using the JSON method. 
Is there anyone who have used this call using C#?

Specifically, I got problems retrieving JSON data using C#. I know, shouldn't be that difficult, but I keep getting a return page from yahoo (?!).

This is the code sample:

    WebClient wc = new WebClient();
    String result = wc.DownloadString("api link goes here");

I also tried using WebRequest() / WebResponse() with the same result, while it works using a WebBrowser control.
It's weird, since yahoo is not my default page and it's not even present in the local cache...

Have you ever tried JSON call with C#? Which method do you use?

Thank you


----------



## mkellogg

Hi,

A redirect to Yahoo?  You are hitting some restrictions we have set up.  What user-agent are you sending?  We probably need to unblock it.

Mike


----------



## uchi.m

Have you ever tried ASP.NET MVC? It's a piece of cake to handle JSON APIs with it


----------



## mcicognani

mkellogg said:


> What user-agent are you sending? We probably need to unblock it.



Oh my! I was getting crazy!

WebClient() or WebRequest() NET API do not have headers by default, so probably they hit your restrictions as well.
I added a standard header and it works!

For reference, this is the instruction to do so:

WebClient wc = new WebClient();
wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; NET CLR 1.0.3705");
String result = wc.DownloadString("api link goes here");

Thank you!


----------



## mkellogg

Good to hear that you got it working.  What were you sending by default, no user-agent or an empty string?


----------



## mcicognani

I'm not sure, but according to MSDN, no user-agent at all.


----------



## mkellogg

Thanks.  I'll check the code to see what we can do to allow that as well.


----------



## mcicognani

uchi.m said:


> Have you ever tried ASP.NET MVC? It's a piece of cake to handle JSON APIs with it



I'm using NET C#, APIs are almost the same. Quite easy to download the data (just three lines as you can see), but not so easy parsing JSON data.
I tried to use DataContractJsonSerializer class, but it doesn't handle very well the format used by Wordreference, mostly because Wordreference return a list of terms and not an array (i.e. not using '[' notation).
So I had to write a specific parser. My choice, I've seen many people use third part libraries anyway, like Newtonking's JSON.Net


----------



## uchi.m

mcicognani said:


> I'm using NET C#, APIs are almost the same. Quite easy to download the data (just three lines as you can see), but not so easy parsing JSON data.
> I tried to use DataContractJsonSerializer class, but it doesn't handle very well the format used by Wordreference, mostly because Wordreference return a list of terms and not an array (i.e. not using '[' notation).
> So I had to write a specific parser. My choice, I've seen many people use third part libraries anyway, like Newtonking's JSON.Net


Check this out.

Ciao


----------

