我正在尝试使用一个使用ASP.NET的这个防火库核心项目来实现get,而我似乎不能返回嵌套在对象中的子项目。我有三个类:路由、Via &Waypoints(充当JSON反序列化的桥梁)。
public class Route
{
public string Route_ID { get; set; }
public string Destination { get; set; }
public string Origin { get; set; }
public Waypoints Stops { get; set; }
public Route()
{
}
}public class Via
{
public string Via_ID { get; set; }
public string Route_ID { get; set; }
public int Seq_Number { get; set; }
public string Coordonnees { get; set; }
public string Description { get; set; }
public Via()
{
}
}public class Waypoints
{
public List<Via> Vias;
public Waypoints()
{
}
}在GET方法中,我从路由中获取所有内容,并希望将其返回为一个JSON列表,其中包含我的所有路由及其路径点,但它只返回一个空的路径点列表:
[HttpGet]
public async Task<IEnumerable<Route>> Get()
{
List<Route> routes = (await firebaseClient
.Child("routes")
.OrderByKey()
.OnceAsync<Route>())
.Select(item =>
new Route
{
Route_ID = item.Key,
Origin = item.Object.Origin,
Destination = item.Object.Destination,
Waypoints = item.Object.Waypoints
}).ToList();
foreach (Route route in routes)
{
List<Via> vias = (await firebaseClient
.Child("routes")
.Child(route.Route_ID)
.Child("Waypoints")
.OrderByKey()
.OnceAsync<Via>())
.Select(waypoint =>
new Via
{
Via_ID = waypoint.Key,
Route_ID = waypoint.Object.Route_ID,
Coordonnees = waypoint.Object.Coordonnees,
Seq_Number = waypoint.Object.Seq_Number,
Description = waypoint.Object.Description
}).ToList();
if(vias.Count > 0)
{
route.Stops.Vias = vias;
}
}
return routes;
}我的数据结构:
{
"routes" : {
"987321": {
"Destination": "13.13;-12.34",
"Origin": "12.12;-12.12",
"Route_ID": "987321",
"Waypoints": {
"4d5e6f": {
"coordonnees": "45.8;-74.7",
"description": "Description",
"route_id": "987321",
"seq_number": 2,
"via_id": "4d5e6f"
},
"111222": {
"coordonnees": "45.8;-74.7",
"description": "Description",
"route_id": "987321",
"seq_number": 1,
"via_id": "111222"
}
}
}
}
}最后我的电话结果是:
[
{
"route_ID": "987321",
"destination": "13.13;-12.34",
"origin": "12.12;-12.12",
"waypoints": {}
}
]反序列化似乎不会比第一层儿童更进一步。有什么解决办法吗?
发布于 2021-08-23 12:22:50
感谢Rena的建议,我发现这个问题位于我的路径点桥接类中,它缺少一个{ get;set;}
下面是对我的班级所做的修改:
public class Waypoints
{
public List<Via> Vias { get; set; }
public Waypoints()
{
}
}https://stackoverflow.com/questions/68866340
复制相似问题