.Net Newton.Json反序列化不起作用



我在asp.net应用程序中将json文件反序列化为模型时遇到了这个问题。所以我有一个Json文件,我从一个名为的api中得到

"Motorola": [
{
"id": 534,
"vendor": "Motorola",
"type": "mobile_phone",
"model": "MOTO Z2 FORCE",
"models": [
{
"condition": "C",
"price": 5269,
"local_price": 29745,
"id": 3407,
"memory": "128 GB",
"color": "lunar_grey",
"product": 534
},
{
"condition": "D",
"price": 4699,
"local_price": 26527,
"id": 3407,
"memory": "128 GB",
"color": "lunar_grey",
"product": 534
}
...

三种型号

public class TradeInResponseModel
{
public string BrandName { get; set; }
public List<TradeInBrandResponseModel> ModelsList { get; set; }
}
public class TradeInBrandResponseModel
{
public int Id { get; set; }
public string Vendor { get; set; }
public string Type { get; set; }
public string ModelName { get; set; }
public List<TradeInProductResponseModel> ModelProducts {get;set;}
}
public class TradeInProductResponseModel
{
public string Condition { get; set; }
public int Price { get; set; }
public int LocalPrice { get; set; }
public int Id { get; set; }
public string Memory { get; set; }
public string Color { get; set; }
public int ProductId { get; set; }
}

但在我运行TradeInResponseModel tradeInResponseModel = JsonConvert.DeserializeObject<TradeInResponseModel>(responseMessage);之后其中responseMessage是来自json文件形式的api的响应型号为null搞不清楚这里出了什么问题。

您的JSON有一个根对象,该对象具有一个名为Motorola而非BrandName的属性。

在JavaScript(和JSON(中,对象实际上是字典。当您告诉序列化程序将JSON数据反序列化为特定对象时,它将尝试将每个字典键与对象属性相匹配。

如果你想将这个JSON解析为字典,你应该使用Dictionary<string,List<TradeInResponseModel>>作为类型,例如:

var brands=JsonConvert.DeserializeObject<Dictionary<string,List<TradeInResponseModel>>>(
responseMessage);

最新更新