将 JSON 对象转换为 JSON 数组时出错



我正在尝试将JSON对象转换为JSON数组,以下是我所做的JSON响应和代码,

链接: http://api.fixer.io/latest?base=USD

JSON 对象:

{  "base": "INR",
   "date": "2017-01-05",
"rates": {
"AUD": 0.020187,
"BGN": 0.027416,
"BRL": 0.047298,
"CAD": 0.019577,
"CHF": 0.015004,
"CNY": 0.10136,
"CZK": 0.37877,
"DKK": 0.10421,
"GBP": 0.011977,
"HKD": 0.11415,
"HRK": 0.10621,
"HUF": 4.3213,
"IDR": 196.67,
"ILS": 0.056738,
"JPY": 1.7155,
"KRW": 17.533,
"MXN": 0.31322,
"MYR": 0.066019,
"NOK": 0.12646,
"NZD": 0.021148,
"PHP": 0.72882,
"PLN": 0.061229,
"RON": 0.06317,
"RUB": 0.87544,
"SEK": 0.13364,
"SGD": 0.021133,
"THB": 0.52705,
"TRY": 0.053295,
"USD": 0.01472,
"ZAR": 0.20079,
"EUR": 0.014018
  }
}

请找到下面的代码

[HttpPost]
    public async Task<ActionResult> Converter(Currency model)
    {
        string URL = "http://api.fixer.io/latest";
        client.BaseAddress = new Uri(URL);
        HttpResponseMessage response = new HttpResponseMessage();
        response = client.GetAsync("?base=USD").Result;
        string json = await response.Content.ReadAsStringAsync();
        JObject jobj = JObject.Parse(json);
        Response rps = JsonConvert.DeserializeObject<Response>(json);
        return View();
    }

以下是使用的类

public class Response
{
    public string based { get; set; }
    public string date { get; set; }
    public List<Rate> rates { get; set; }
}
public class Rate
{
    public string AUD {get; set;}
    public string BGN {get; set;}
    public string BRL {get; set;}
    public string CAD {get; set;}
    public string CHF {get; set;}
    public string CNY {get; set;}
    public string CZK {get; set;}
    public string DKK {get; set;}
    public string GBP {get; set;}
    public string HKD {get; set;}
    public string HRK {get; set;}
    public string HUF {get; set;}
    public string IDR {get; set;}
    public string ILS {get; set;}
    public string JPY {get; set;}
    public string KRW {get; set;}
    public string MXN {get; set;}
    public string MYR {get; set;}
    public string NOK {get; set;}
    public string NZD {get; set;}
    public string PHP {get; set;}
    public string PLN {get; set;}
    public string RON {get; set;}
    public string RUB {get; set;}
    public string SEK {get; set;}
    public string SGD {get; set;}
    public string THB {get; set;}
    public string TRY {get; set;}
    public string USD {get; set;}
    public string ZAR {get; set;}
    public string EUR {get; set;}
}

下面是我得到的错误

"/"应用程序中的服务器错误。

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type     
'System.Collections.Generic.List`1[CurrencyConversion.Models.Rate]' because 
the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or 
change the deserialized type so that it is a normal .NET type (e.g. not a
primitive type like integer, not a collection type like an array or List<T>) 
that can be deserialized from a JSON object. JsonObjectAttribute can also be 
added to the type to force it to deserialize from a JSON object.
Path 'rates.AUD', line 1, position 49.  

费率不是一个数组,只是一个对象。所以改变

public List<Rate> rates { get; set; }

public Dictionary<string,double> rates { get; set; }

这样,您就不需要这个大的费率类。

您的最后一堂课

public class Response
{
    public string based { get; set; }
    public string date { get; set; }
    public Dictionary<string,double> rates { get; set; }
}

顺便说一句:你的代码中不需要JObect.Parse。您可以将 json 字符串直接提供给 JsonConvert.DeserializeObject

您没有 Rate 列表 您有一个 Rate 对象(在您的 json 中)

试试这个

public class Response
{
    public string @base { get; set; }
    public string date { get; set; }
    public Rate rates { get; set; }
}

此外,请注意,您在Responsebased属性当前不会映射到您的 JSON。在你的 JSON 中,它是base
base 是一个保留关键字,因此在我的 Response 类版本中使用 @base 属性

相关内容

  • 没有找到相关文章

最新更新