2016-05-31 66 views
1

我有这样的API调用:如何找出字典对象中键的值?

HttpResponse<string> response = 
    Unirest.get("https://wordsapiv1.p.mashape.com/words/" + word.Name) 
    .header("X-Mashape-Key", "xxxx") 
    .header("Accept", "application/json") 
    .asJson<string>(); 

下面是HttpResponse类:

public class HttpResponse<T> 
{ 
    public HttpResponse(HttpResponseMessage response); 

    public T Body { get; set; } 
    public int Code { get; } 
    public Dictionary<string, string> Headers { get; } 
    public Stream Raw { get; } 
} 

我没有问题得到机构(response.Body)或代码,但我想做些什么是得到这个标头:

[7] = {[X-RateLimit-requests-Remaining, 2498]} 

有人可以告诉我怎么可以检查返回的响应,并找出值的X-RateLimit-requests-Remaining

+1

如何:'response.Headers [“X-RateLimit - 请求 - 剩余“]'?所以你知道字典是如何工作的吗? – ckruczek

+0

这是什么'Unirest',为什么它的方法不遵循.NET命名约定? –

+0

@MattiVirkkunen这是一个[轻量级的HTTP请求库](http://unirest.io/net.html),命名可能是由于兼容性原因与其他支持的语言。 –

回答

4

词典有一些东西叫indexer。索引器的数据类型是KeyDictionary<Key,Value>)的数据类型。

索引器类似于属性的getter和setter,并且这样实现的:

public TValue this[TKey index] 
{ 
    // this will return when being called e.g. 'var x = dictionary[key];' 
    get { return whatever; } 

    // and here 'value' is whatever you pass to the setter e.g. 'dictionary[kex] = x;' 
    set { whatever = value; } 
} 

在你的情况下,这将是:

// "Key" is just an example, use "X-RateLimit-requests-Remaining" instead ;) 
response.Headers["Key"];