我想使用 CosmosDB 容器中的数据创建一个 API 端点。数据结构如下:
{
"id": "28.02.2022 09:31:42",
"time": 1646037099,
"states": [
[
"aa56db",
"UAL339 ",
"United States",
1646037098,
1646037099,
-84.9531,
25.9777,
11277.6,
false,
239.52,
25.85,
0,
null,
11811,
null,
false,
0,
4
],
[
"3c403f",
"FME ",
"Germany",
1646037012,
1646037012,
13.5172,
52.3712,
null,
true,
5.14,
247.5,
null,
null,
null,
null,
false,
0,
17
],
首先,我创建了如下所示的模型“OpenSky”:
public class OpenSky
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("time")]
public decimal? Time { get; set; }
[JsonProperty("states")]
public List<List<**OpenSkyFlight**>> States { get; set; }
}
属性状态是对象类型OpenSkyFlight的列表列表。OpenSkyFlight 模型如下所示:
public class OpenSkyFlight
{
public string Icao24 { get; set; }
public string Callsign { get; set; }
public string OriginCountry { get; set; }
public int? TimePosition { get; set; }
public int? LastContact { get; set; }
public float? Longitude { get; set; }
public float? Latitude { get; set; }
public float? BaroAltitude { get; set; }
public bool? OnGround { get; set; }
public float? Velocity { get; set; }
public float? TrueTrack { get; set; }
public float? VerticalRate { get; set; }
public string Sensors { get; set; }
public float? GeoAltitude { get; set; }
public string Squawk { get; set; }
public bool? Spi { get; set; }
public int? PositionSource { get; set; }
}
问题是我不知道如何将这些 json 属性“状态”扁平化为像 OpenSkyFlight 这样的对象类型。我在 Context.cs 中尝试了以下内容:
// OpenSky
modelBuilder.Entity<OpenSky>()
.Property(p => p.Id)
.ToJsonProperty("id");
modelBuilder.Entity<OpenSky>()
.Property(p => p.Time)
.ToJsonProperty("time");
modelBuilder.Entity<OpenSky>()
.Property(p => p.States)
.HasConversion(
v => JsonConvert.SerializeObject(v),
v => JsonConvert.DeserializeObject<List<List<object>>>(v => new Icao24 = JsonConvert.ToString(v[0]))
)
.ToJsonProperty("states");
先感谢您!