首页 > 解决方案 > 如何从嵌套对象中获取属性?

问题描述

我是 c# 语言的新手,我有 json 数据并转换为 c# 对象,但我想从响应模型和注释中获取嵌套属性以及来自响应类的所有数据,我还需要使用 foreach 循环从响应类中获取数据

public class Response
{
    public string comments { get; set; }
    public string createDate { get; set; }
    public string status { get; set; }
    public string updateDate { get; set; }
}

public class Reviewer
{
    public string displayName { get; set; }
    public string oneAccountId { get; set; }
}

public class Vehicle
{
    public string make { get; set; }
    public string model { get; set; }
    public string vin { get; set; }
    public string year { get; set; }
}

public class ShopReview
{
    public string comments { get; set; }
    public string compCode { get; set; }
    public string createDate { get; set; }
    public string id { get; set; }
    public int rating { get; set; }
    public Response response { get; set; }
    public Reviewer reviewer { get; set; }
    public string status { get; set; }

}

public class RootObject
{
    public string message { get; set; }
    public ShopReview[] shopReviews { get; set; }
}

我尝试使用此代码

   if (review.Response != null)
  {
     foreach (var shopResponse in review.Response)
     {
     CarReviewReply tmpReply = new CarReviewReply();
      {
      Comments = shResponse.Comments,
      LastUpdated = shopResponse.UpdateDate,
      ResponseDate = shopResponse.CreateDate,
      Status = shopResponse.Status
      };
  }
}

我收到错误“不包含 'GetEnumerator' 的公共实例定义”

标签: c#asp.netarraysnestedjson.net

解决方案


推荐阅读