寻找带有JSON客户端库的REST

时间:2011-12-05 17:36:35

标签: c# json web-services rest .net-3.5

我需要连接到通过REST接口提供JSON的端点。我无法找到任何以连贯的方式结合这两种技术的东西。

我正在寻找一个能让我快速入门的图书馆。

4 个答案:

答案 0 :(得分:8)

您可以使用Json.Net库和使用DynamicObject的{​​{3}}

一些用法示例:

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);
    }
}

修改

这是另一个没有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");
        }
    }
}

答案 1 :(得分:4)

我会看看RestSharp。这是非常直接的起床和运行,并有一个积极的跟随。

入门指南:https://github.com/restsharp/RestSharp/wiki

反序列化:https://github.com/restsharp/RestSharp/wiki/Deserialization

答案 2 :(得分:1)

WCF Web API中的HttpCLient和JSONValue类型可以帮助您实现目标。下载源代码并查看示例。在客户端上有许多用于使用JSON的示例。 http://wcf.codeplex.com/releases

另见

http://blog.alexonasp.net/

答案 3 :(得分:1)

ServiceStack.Text可能是最简单的方法之一。

背景:ServiceStack.Text是一个独立的,无依赖的序列化库,包含ServiceStack的文本处理功能

示例

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>();

我认为PostJsonToUrlFromJson扩展方法是一些很好的语法糖。