【发布时间】:2020-06-12 20:34:17
【问题描述】:
我无法在 razor 页面上列出 json,这给了我这个问题:
JsonSerializationException: 无法反序列化当前 JSON 数组 (例如 [1,2,3])输入“JsonDisplayASP.Models.ListRates”,因为 type 需要一个 JSON 对象(例如 {"name":"value"})来反序列化 正确。
json:
[{"from":"USD","to":"AUD","rate":"0.93"},{"from":"AUD","to":"USD","rate":"1.08"}]
rates控制器:
public class rates
{
public rates(string from, string to, double rate)
{
this.from = from;
this.to = to;
this.rate = rate;
}
[JsonPropertyName("from")]
public string from { get; set; }
[JsonPropertyName("to")]
public string to { get; set; }
[JsonPropertyName("rate")]
public double rate { get; set; }
public class ListRates
{
public List<rates> LRates { get; set; }
}
index.cshtml:
@model JsonDisplayASP.Models.ListRates
@foreach (var item in Model.LRates)
{
var from = item.from;
var to = item.to;
var rate = item.rate;
我该怎么做?她不让我给她看风景
【问题讨论】:
-
我想你忘了在反序列化 json 的地方包含代码,这真的很重要
标签: asp.net json razor model deserialization