Looking for a REST with JSON client library [closed]
Asked Answered
O

4

7

I need to connect to an endpoint that serves out JSON via REST interfaces. I can't really find anything that combines these 2 technologies in a coherent manner.

I am looking for a library that will let me get started quickly.

Ortego answered 5/12, 2011 at 17:36 Comment(2)
A few blogs show how to do this using WCF encrypted.google.com/search?q=wcf+rest+jsonOverglaze
@Overglaze Most of the links detail how to create a service that hands out JSON. I need a library for easily consuming it.Ortego
E
8

You can use Json.Net library and this extension class that makes use of DynamicObject

Some usage examples:

public static void GoogleGeoCode(string address)
{
    string url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";
    dynamic googleResults = new Uri(url + address).GetDynamicJsonObject();

    foreach (var result in googleResults.results)
    {
        Console.WriteLine("[" + result.geometry.location.lat + "," + 
                                result.geometry.location.lng + "] " + 
                                result.formatted_address);
    }
}

public static void GoogleSearch(string keyword)
{
    string url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=8&q=";
    dynamic googleResults = new Uri(url + keyword).GetDynamicJsonObject();

    foreach (var result in googleResults.responseData.results)
    {
        Console.WriteLine(
            result.titleNoFormatting + "\n" + 
            result.content + "\n" + 
            result.unescapedUrl + "\n");
    }
}

public static void Twitter(string screenName)
{
    string url = "https://api.twitter.com/1/users/lookup.json?screen_name=" + screenName;
    dynamic result = new Uri(url).GetDynamicJsonObject();
    foreach (var entry in result)
    {
        Console.WriteLine(entry.name + " " + entry.status.created_at);
    }
}

public static void Wikipedia(string query)
{
    string url = "http://en.wikipedia.org/w/api.php?action=opensearch&search=" + query +"&format=json";
    dynamic result = new Uri(url).GetDynamicJsonObject();

    Console.WriteLine("QUESTION: " + result[0]);
    foreach (var entry in result[1])
    {
        Console.WriteLine("ANSWER: " + entry);
    }
}

EDIT:

Here is another sample without DynamicObject

public static void GoogleSearch2(string keyword)
{
    string url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=8&q="+keyword;

    using(WebClient wc = new WebClient())
    {
        wc.Encoding = System.Text.Encoding.UTF8;
        wc.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)";
        string jsonStr = wc.DownloadString(url);
        JObject jObject = (JObject)JsonConvert.DeserializeObject(jsonStr);
        foreach (JObject result in jObject["responseData"]["results"])
        {
            Console.WriteLine(
                result["titleNoFormatting"] + "\n" +
                result["content"] + "\n" +
                result["unescapedUrl"] + "\n");
        }
    }
}
Endoscope answered 5/12, 2011 at 18:37 Comment(3)
Unfortunately, I am stuck with .NET 3.5 as per tag, thus can't use dynamic.Ortego
Sorry, I missed that. But you can still use Json.Net library to parse the json strings returned (JObject.Parse or JsonConvert.DeserializeObject)Endoscope
@AngryHacker, I edited my answer, avoiding .NET4 features.Endoscope
A
4

I would take a look at RestSharp. It's very straight forward to get up and running and has an active following.

Getting started guide: https://github.com/restsharp/RestSharp/wiki

Deserialization: https://github.com/restsharp/RestSharp/wiki/Deserialization

Ascending answered 5/12, 2011 at 18:23 Comment(0)
F
1

The HttpCLient and the JSONValue Type from the WCF Web API should get you on your way. Download the source and look at the samples. There are many samples for working with JSON on the client. http://wcf.codeplex.com/releases

Also see

http://blog.alexonasp.net/

Flushing answered 5/12, 2011 at 18:14 Comment(0)
N
1

ServiceStack.Text is probably one of the easiest ways to do this.

Background: ServiceStack.Text is an independent, dependency-free serialization library that contains ServiceStack's text processing functionality

Example

using ServiceStack.Text;

//  Create our arguments object:
object args = new
{
   your = "Some",
   properties = "Other",
   here = "Value",
};

var resultString = fullUrl.PostJsonToUrl(args);
results = resultString.Trim().FromJson<T>();

The PostJsonToUrl and FromJson extension methods are some nice syntactic sugar in my opinion.

Nonce answered 7/2, 2014 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.