Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么从json反序列化的web api数据返回为null而不显示在视图中?_C#_Asp.net Mvc_Asp.net Web Api - Fatal编程技术网

C# 为什么从json反序列化的web api数据返回为null而不显示在视图中?

C# 为什么从json反序列化的web api数据返回为null而不显示在视图中?,c#,asp.net-mvc,asp.net-web-api,C#,Asp.net Mvc,Asp.net Web Api,我不熟悉JSON序列化和反序列化,目前正在尝试从Web API调用反序列化一组数据。数据在反序列化之前是可见的,但是当它被发送到视图时,它显示为“null” 型号: public class Currency { public string Rates { get; set; } } 控制器: public class CurrencyController:Controller { string Baseurl = "https://api.exchangeratesapi.

我不熟悉JSON序列化和反序列化,目前正在尝试从Web API调用反序列化一组数据。数据在反序列化之前是可见的,但是当它被发送到视图时,它显示为“null”

型号:

public class Currency
{
    public string Rates { get; set; }
}
控制器:

public class CurrencyController:Controller
{ 
    string Baseurl = "https://api.exchangeratesapi.io/";

    public async Task<ActionResult> Index()
    {
        Currency CurencyInfo = new Currency();

        using (HttpClient client = new HttpClient())
        {
            client.BaseAddress = new Uri(Baseurl);
            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage Result = await client.GetAsync("https://api.exchangeratesapi.io/latest");

            if (Result.IsSuccessStatusCode)
            {
                // Storing the response details received from Web API 
                var CurrencyResponse = Result.Content.ReadAsStringAsync().Result;

                // Deserializing the response received from Web API and storing into the CurrencyInfo 
                CurencyInfo = JsonConvert.DeserializeObject<Currency>((JObject.Parse(CurrencyResponse)["rates"]).ToString());
            }

            // returning the Currency Info to view  
            return View(CurencyInfo);
        }
    }
}
公共类CurrencyController:控制器
{ 
字符串Baseurl=”https://api.exchangeratesapi.io/";
公共异步任务索引()
{
Currency CurencyInfo=新货币();
使用(HttpClient=new HttpClient())
{
client.BaseAddress=新Uri(Baseurl);
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
HttpResponseMessage结果=等待客户端。GetAsync(“https://api.exchangeratesapi.io/latest");
if(结果。IsSuccessStatusCode)
{
//存储从Web API接收的响应详细信息
var CurrencyResponse=Result.Content.ReadAsStringAsync().Result;
//反序列化从Web API接收的响应并存储到CurrencyInfo中
CurencyInfo=JsonConvert.DeserializeObject((JObject.Parse(CurrencyResponse)[“rates”]).ToString());
}
//返回要查看的货币信息
返回视图(CurencyInfo);
}
}
}

您的货币类不正确。它不包括要绑定的所有属性。 查看由返回的JSON结果

您的货币类别应如下所示:

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

public class Currency
{
    public Rates rates { get; set; }

}
 public async Task<ActionResult> Index()
        {
            Currency CurencyInfo = new Currency();

            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(Baseurl);

                client.DefaultRequestHeaders.Clear();

                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                Uri myUri = new Uri("https://api.exchangeratesapi.io/latest", UriKind.Absolute);

                HttpResponseMessage Result = await client.GetAsync(myUri);

                if (Result.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api   
                    var CurrencyResponse = Result.Content.ReadAsStringAsync().Result;


                    //Deserializing the response recieved from web api and storing into the CurrencyInfo 
                    CurencyInfo = JsonConvert.DeserializeObject<Currency>((JObject.Parse(CurrencyResponse)).ToString());

                }

                //returning the Currency Info to view  
                return View(CurencyInfo);
            }

        }
您的操作方法应如下所示:

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

public class Currency
{
    public Rates rates { get; set; }

}
 public async Task<ActionResult> Index()
        {
            Currency CurencyInfo = new Currency();

            using (HttpClient client = new HttpClient())
            {
                client.BaseAddress = new Uri(Baseurl);

                client.DefaultRequestHeaders.Clear();

                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                Uri myUri = new Uri("https://api.exchangeratesapi.io/latest", UriKind.Absolute);

                HttpResponseMessage Result = await client.GetAsync(myUri);

                if (Result.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api   
                    var CurrencyResponse = Result.Content.ReadAsStringAsync().Result;


                    //Deserializing the response recieved from web api and storing into the CurrencyInfo 
                    CurencyInfo = JsonConvert.DeserializeObject<Currency>((JObject.Parse(CurrencyResponse)).ToString());

                }

                //returning the Currency Info to view  
                return View(CurencyInfo);
            }

        }
公共异步任务


您可以使用任何在线工具(如json2sharp)来为要反序列化的JSON数据构建类/模型

欢迎使用SO,您的模型与该API返回的数据不匹配。CurrencyResponse具有所有返回值,但在反序列化后,CurrencyInfo返回null,即rates请更新您的问题以添加两位信息。a)
CurrencyResponse
的确切值。b)
(JObject.Parse(CurrencyResponse)[“rates”]).ToString()的确切值
。:1.4581,“HKD\”:8.7295,,““HKD\”:8.7295,““ISK\”:8.7295,,““ISK\”::139.3,,““PHP\”:56.969,“;PHP\”:56.969,:56.969.969,“,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,4405,“日元”:120.87,“泰铢”:33.735,“瑞士法郎”:1.1004,“新加坡元\1.5164岁,1.5164岁,1.5164岁,印尼“PLN”印尼:4.2778,“BGN\”:1.9558,,“尝试”尝试:6.4897,,“人民币:7.8795,,,“诺克”矿工:10.1833,,“诺克”:10.1833,,“NZZZZZZD\”:10.515164,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,信息{CurrencyInformer.Models.Currency}费率:nullThank虽然问题出在ASP.net mvc上,而且它仍然存在,但您已经向我展示了解决问题的方法。非常感谢。与其列出每种货币,我建议使用。Thank@mjwills,但现在我尝试在ASP.net mvc视图上显示数据,我可以在控制器中获取数据,但无法转换n由于IEnumerable错误,请将其传递到视图。@Kunal您使用的是哪种方法?我回答的方法还是@mjwills推荐的方法?@Tango我使用的是您使用过的方法,但经常遇到此错误“传入字典的模型项的类型为'CurrencyInformer.Models.Currency',但此词典需要类型为“System.Collections.Generic.IEnumerable`1[CurrencyInformer.Models.Currency]”的模型项。“即使将模型变量更改为ienumable类型。此错误是在我调整了您的方法之后发生的。”。