在反序列化到类模型期间处理JSON数据

时间:2019-09-24 13:30:36

标签: c# json

反序列化时是否可以将两个JSON字段一起添加?

例如:

public class root{
    public List<cars> carList{get;set;}
}

public class cars{
    public int HondaValue{get;set;}
    public int fordValue{get;set;}
    public int totalValue{get;set;}
}

JSON:

{
  "carList":
    {
     "hondaValue":30,000,
     "fordValue":40,000,
    },
    {
     "hondaValue":55,000,
     "fordValue":62,000,
    },
    {
     "hondaValue":77,000,
     "fordValue":65,000,
    },
}

那么在反序列化期间,我可以在类模型中获得hondaValue和fordValue的总和吗?还是需要在反序列化之后将它们设置为模型值,然后将其添加/设置为总计?

1 个答案:

答案 0 :(得分:3)

您不需要像这样做foreach:

public struct Cars
{    
   public int HondaValue { get; set; }    
   public int FordValue { get; set; }
   public int TotalValue 
   { 
      get 
      {
         return HondaValue + FordValue;
      }
   }
}
相关问题