문제

I have an api which returns the json as { "timestamp": 1372741243, "base": "USD" }

I would like to call this using

        HttpResponseMessage response = client.GetAsync("api/latest.json?app_id=your_api_id").Result;
        if (response.IsSuccessStatusCode)
        {
            var curr = response.Content.ReadAsAsync<Currency>().Result;
        }

Now, the base in the api cannot be parsed as I cannot have base property in the Currency class, as base is a key-word. Any suggestions to overcome this.

도움이 되었습니까?

해결책

try with adding prefix "@" before the parameter name base

public string @base { get; set; }

다른 팁

You can have a property named base, you just need to prefix the identifier with an @ sign. From the C# Language Specification, 2.4.2 Identifiers:

The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier.

Try declaring your Currency like this:

public class Currency
{
    public long timestamp { get; set; }
    public string @base { get; set; }
    ...
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top