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

C# 将JSON对象转换为JSON数组时出错,c#,json,json.net,C#,Json,Json.net,我正在尝试将JSON对象转换为JSON数组,下面是我所做的JSON响应和代码 链接: 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.011

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

链接:

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();
    }
[HttpPost]
公共异步任务转换器(货币模型)
{
字符串URL=”http://api.fixer.io/latest";
client.BaseAddress=新Uri(URL);
HttpResponseMessage response=新的HttpResponseMessage();
response=client.GetAsync(“?base=USD”).Result;
string json=wait response.Content.ReadAsStringAsync();
JObject-jobj=JObject.Parse(json);
响应rps=JsonConvert.DeserializeObject(json);
返回视图();
}
下面是使用的类

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;}
}
公共类响应
{
基于公共字符串的{get;set;}
公共字符串日期{get;set;}
公共列表比率{get;set;}
}
公费
{
公共字符串AUD{get;set;}
公共字符串BGN{get;set;}
公共字符串BRL{get;set;}
公共字符串CAD{get;set;}
公共字符串CHF{get;set;}
公共字符串CNY{get;set;}
公共字符串CZK{get;set;}
公共字符串DKK{get;set;}
公共字符串GBP{get;set;}
公共字符串HKD{get;set;}
公共字符串HRK{get;set;}
公共字符串HUF{get;set;}
公共字符串IDR{get;set;}
公共字符串ILS{get;set;}
公共字符串JPY{get;set;}
公共字符串KRW{get;set;}
公共字符串MXN{get;set;}
公共字符串MYR{get;set;}
公共字符串NOK{get;set;}
公共字符串NZD{get;set;}
公共字符串PHP{get;set;}
公共字符串PLN{get;set;}
公共字符串RON{get;set;}
公共字符串{get;set;}
公共字符串SEK{get;set;}
公共字符串SGD{get;set;}
公共字符串THB{get;set;}
公共字符串TRY{get;set;}
公共字符串USD{get;set;}
公共字符串ZAR{get;set;}
公共字符串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.  
无法将当前JSON对象(例如{“name”:“value”})反序列化为类型
'System.Collections.Generic.List'1[CurrencyConversion.Models.Rate]'因为
该类型需要一个JSON数组(例如[1,2,3])才能正确反序列化。
要修复此错误,请将JSON更改为JSON数组(例如[1,2,3]),或
更改反序列化类型,使其成为正常的.NET类型(例如,不是
基本类型(如整数,而不是数组或列表等集合类型)
可以从JSON对象反序列化的。JsonObjectAttribute也可以是
添加到类型以强制其从JSON对象反序列化。
路径“rates.AUD”,第1行,位置49。

您没有速率列表您有一个速率对象(在json中)

试试这个

public class Response
{
    public string @base { get; set; }
    public string date { get; set; }
    public Rate rates { get; set; }
}
另外,请注意,
响应
中基于
属性当前不会映射到JSON。 在JSON中,它是
base

base
是一个保留关键字,因此我的
Response
类版本中的
@base
属性不是数组,只是一个对象。所以改变

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

公共字典率{get;set;}
那样的话,你就不需要这个高费率的课程了

你的最后一节课

public class Response
{
    public string based { get; set; }
    public string date { get; set; }
    public Dictionary<string,double> rates { get; set; }
}
公共类响应
{
基于公共字符串的{get;set;}
公共字符串日期{get;set;}
公共字典率{get;set;}
}

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

您确定json将绑定到字典而不使用
属性吗?@Developer我确定also@DeveloperJSO本质上是一个字典,在JSON.org上描述为“名称/值对的集合。在不同的语言中,这是作为对象、记录、结构、字典、哈希表、键控列表或关联数组来实现的。”这个答案还解决了
base
mis映射问题,而当前上浮的答案没有。对,但它对于未来添加/删除的货币来说更具弹性。因此,我投票支持另一个答案。
public class Response
{
    public string based { get; set; }
    public string date { get; set; }
    public Dictionary<string,double> rates { get; set; }
}