寻找一个带有JSON的REST客户端库

本文关键字:JSON REST 客户端 一个 寻找 | 更新日期: 2023-09-27 18:16:46

我需要连接到通过REST接口提供JSON的端点。我真的找不到任何东西可以将这两种技术以一种连贯的方式结合起来。

我正在寻找一个库,将让我快速开始。

寻找一个带有JSON的REST客户端库

可以使用Json。. Net库和这个使用DynamicObject

的扩展类

一些用法示例:

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

我会看看RestSharp。这是非常直接的启动和运行,并有一个积极的追随者。

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

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

来自WCF Web API的HttpCLient和JSONValue类型应该会让您上路。下载源代码并查看示例。有许多在客户机上使用JSON的示例。http://wcf.codeplex.com/releases

也看到http://blog.alexonasp.net/

ServiceStack。文本可能是最简单的方法之一。

: 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扩展方法在我看来是一些很好的语法糖。