这样的请求在代码中是如何解释的?



我有一个JSON请求,像这样:

{
"locations" : [
"53.44059300,-2.22992800",
"53.36246000,-2.26683200"
],
}

如何从Postmana获得:

var body = @"{
" + "n" +
@"  ""locations"" : [
" + "n" +
@"    ""53.44059300,-2.22992800"",
" + "n" +
@"    ""53.36246000,-2.26683200""
" + "n" +
@"  ]
" + "n" +  @"}";

现在我想将字符串查询重写为结构化查询:

var request = new Request();
foreach (var address in incomingRequest.Addresses)
{
request .Locations.Add(new Locations
{
Latitude = address.Latitude,
Longitude = address.Longitude
});
}

请求类看起来像这样:

internal class Request : DerivedA
{
public List<Locations> Locations { get; set; } = new List<Locations>();
}

但是,最后,我的输出与初始请求不同:

{
"Locations":[
{
"Latitude":51.469575800000,
"Longitude":-0.449607200000
},
{
"Latitude":53.361936300000,
"Longitude":-2.272971700000
}
]
}

这只是一个字符串:

"53.44059300,-2.22992800"

那么locations就是一个字符串数组:

internal class Request : DerivedA
{
public List<string> Locations { get; set; } = new List<string>();
}

用字符串填充:

var request = new Request();
foreach (var address in incomingRequest.Addresses)
{
request.Locations.Add($"{address.Latitude},{address.Longitude}");
}

作为题外话…

命名很重要。您目前有(但可能不再需要)一个名为Locations的类,它表示单个位置。混淆复数是一个等待发生的错误。