c# - 如何从嵌套对象中获取属性?
问题描述
我是 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' 的公共实例定义”
解决方案
推荐阅读
- python - Python - 使用多个字段对单个 csv 文件进行排序并删除该文件中连续的行我如何使用 sqlite 执行此操作
- python - 如何解决 Python 的“AttributeError: 'tuple' object has no attribute 'value'”?
- reactjs - 如何使用 React 和 WebPack 使 SourceMaps 适用于 1p 包?
- cassandra - 是否可以增加 cassandra 中单元格的大小?
- java - 将数组表示为多项式
- .htaccess - .htaccess 动态 URL 到静态 URL
- swift - 保存 UICollectioView 用户进度 [Swift]
- javascript - PHP build_calendar 不显示表格
- python - 如何在 Tkinter 中更新标签
- python - 使用 vb6 winsock 发送 python 字符串