Domanda

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.

È stato utile?

Soluzione

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

public string @base { get; set; }

Altri suggerimenti

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; }
    ...
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top