如何找出字典对象中键的值?

时间:2016-05-31 08:24:44

标签: c#

我有这个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 个答案:

答案 0 :(得分: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"];