I'm trying to teach myself working with APIs, and chose to try and make something that fetches currency exchange rates from a certain free API regarding just that. I've managed to successfully make the API call and record the JSON data from it, and I'd like to save the data so it's easily accessible from an object.
My issue is this; the JSON data reads like
{
"Realtime Currency Exchange Rate": {
"1. From_Currency Code": "USD",
"2. From_Currency Name": "United States Dollar",
"3. To_Currency Code": "EUR",
"4. To_Currency Name": "Euro",
"5. Exchange Rate": "0.87490000",
"6. Last Refreshed": "2020-07-19 08:16:36",
"7. Time Zone": "UTC",
"8. Bid Price": "0.87490000",
"9. Ask Price": "0.87540000"
}
}
Using https://json2csharp.com/ I'm getting a result like this
public class RealtimeCurrencyExchangeRate {
public string 1.From_CurrencyCode { get; set; }
public string 2.From_CurrencyName { get; set; }
public string 3.To_CurrencyCode { get; set; }
public string 4.To_CurrencyName { get; set; }
public string 5.ExchangeRate { get; set; }
public string 6.LastRefreshed { get; set; }
public string 7.TimeZone { get; set; }
public string 8.BidPrice { get; set; }
public string 9.AskPrice { get; set; }
}
Which isn't valid c# syntax. The JSON parser I have could deserialize that info (I think) but Visual Studio is throwing a well deserved fit over me trying to name a variable 1.From_CurrencyCode
You can use the JsonProperty
attribute (if you're using Newtonsoft.Json) to decorate each of your model properties. This allows you to rename the Json property names to valid C# property names.
public class RealtimeCurrencyExchangeRate
{
[JsonProperty("1. From_Currency Code")]
public string FromCurrencyCode { get; set; }
// similarly for the remaining properties
...
}
// also apply it to your "root" class
public class Root
{
[JsonProperty("Realtime Currency Exchange Rate")]
public RealtimeCurrencyExchangeRate RealtimeCurrencyExchangeRate { get; set; }
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다