【发布时间】:2019-03-22 23:51:35
【问题描述】:
我有以下课程:
public class Product
{
[JsonProperty("image")]
public string ImageURL { get; set; }
[JsonProperty("superDepartment")]
public string SuperDepartment { get; set; }
[JsonProperty("tpnb")]
public long TPNB { get; set; }
[JsonProperty("ContentsMeasureType")]
public string ContentsMeasureType { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("UnitOfSale")]
public int UnitOfSale { get; set; }
[JsonProperty("description")]
public IEnumerator<string> LstDescription { get; set; }
public string Description { get; set; }
[JsonProperty("AverageSellingUnitWeight")]
public decimal AverageSellingUnitWeight { get; set; }
[JsonProperty("UnitQuantity")]
public string UnitQuantity { get; set; }
[JsonProperty("id")]
public long ID { get; set; }
[JsonProperty("ContentsQuantity")]
public int ContentsQuantity { get; set; }
[JsonProperty("department")]
public string Department { get; set; }
[JsonProperty("price")]
public decimal Price { get; set; }
[JsonProperty("unitprice")]
public decimal UnitPrice { get; set; }
}
我在productController上有方法:
public async Task<ActionResult> MakeRequest(string q)// worked
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "56ac439a92694577a2779f3d0ee0cd85");
var uri = string.Format("https://dev.tescolabs.com/grocery/products/?query={0}&offset={1}&limit={2}", q, 0, 10);
var response = await client.GetAsync(uri);
string body = await response.Content.ReadAsStringAsync();
Product myoutput = JsonConvert.DeserializeObject<Product>(body);
}
return View(body);
}
我有下面的模型:
由于某种原因,当我调用 MakeRequest 方法时,它只将信息显示为字符串,并显示服务器错误,如下图所示,我们可以从 api 中看到信息,但它显示为字符串。
error message
来自 api 的信息应显示在下表中:
table
如何在表格中显示数据?或者更好的如何将 json 数组转换为 net 对象?
我知道我的方法下面的部分缺少一些东西:
Product myoutput = JsonConvert.DeserializeObject<Product>(body);
【问题讨论】:
-
您的模型与响应 json 模型不匹配。
-
你好,Sinan,非常感谢你能给我更多关于模型模型不匹配响应 json 的信息吗?
标签: c# .net json api model-view-controller