我的JSON字符串未反序列化为C#对象

时间:2019-03-18 20:47:48

标签: c# json api serialization

我正在从货币API中获取JSON字符串,并尝试将其存储在模型类中。当获取JSON但未将其反序列化为对象并且每个值显示零值0时,就会发生问题。

protected void Convert(object sender, EventArgs e)
{
    string url = string.Format("https://api.exchangeratesapi.io/latest", ddlFrom.SelectedItem.Value, ddlTo.SelectedItem.Value);

    WebClient client = new WebClient();
    string rates =client.DownloadString(url);
    string[] values = Regex.Split(rates, ",");

    Rate rate = JsonConvert.DeserializeObject<Rate>(rates);
}

我的模型课是:

public class Rate
{
    public double JPY { get; set; }
    public double BRL { get; set; }
    public double NOK { get; set; }
    public double DKK { get; set; }
    public double RUB { get; set; }
    public double BGN { get; set; }
    public double SEK { get; set; }
    public double CHF { get; set; }
    public double PHP { get; set; }
    public double AUD { get; set; }
    public double CZK { get; set; }
    public double ZAR { get; set; }
    public double USD { get; set; }
    public double TRY { get; set; }
    public double RON { get; set; }
    public double MXN { get; set; }
    public double PLN { get; set; }
    public double THB { get; set; }
    public double ISK { get; set; }
    public double CAD { get; set; }
    public double HKD { get; set; }
    public double NZD { get; set; }
    public double KRW { get; set; }
    public double SGD { get; set; }
    public double MYR { get; set; }
    public double HUF { get; set; }
    public double GBP { get; set; }
    public double HRK { get; set; }
    public double IDR { get; set; }
    public double INR { get; set; }
    public double ILS { get; set; }
    public double CNY { get; set; }
}

public class RootObject
{
    public double rates { get; set; }
    public string date { get; set; }
    public string @base { get; set; }
}

JSON字符串是:

{
    "rates": {
        "JPY": 126.54,
        "BRL": 4.3323,
        "NOK": 9.672,
        "DKK": 7.4621,
        "RUB": 72.9217,
        "BGN": 1.9558,
        "SEK": 10.4643,
        "CHF": 1.136,
        "PHP": 59.809,
        "AUD": 1.5981,
        "CZK": 25.616,
        "ZAR": 16.3174,
        "USD": 1.1349,
        "TRY": 6.1978,
        "RON": 4.7543,
        "MXN": 21.7484,
        "PLN": 4.2979,
        "THB": 35.954,
        "ISK": 133.1,
        "CAD": 1.5134,
        "HKD": 8.9089,
        "NZD": 1.6537,
        "KRW": 1284.66,
        "SGD": 1.5335,
        "MYR": 4.6263,
        "HUF": 314.38,
        "GBP": 0.8566,
        "HRK": 7.4148,
        "IDR": 16158.0,
        "INR": 77.781,
        "ILS": 4.0902,
        "CNY": 7.6186
    },
    "date": "2019-03-18",
    "base": "EUR"
}

0 个答案:

没有答案