如何使用 json 解析路由特征权重响应?
How to parse route feature weightings response with json?
根据 API 文档 (https://developer.here.com/rest-apis/documentation/routing/topics/resource-param-type-routing-mode.html),路线要素权重显示为 RouteFeatureType: RouteFeatureWeightType
对列表。
如果我想在一个calculateroute
请求中设置2个或更多权重,即
"motorway:-2,boatFerry:-3
我在 CalculateRouteResponseType
响应中得到了结果路线的相关部分:
\"feature\":
[{\"value\":\"boatFerry\",\"weight\":[\"Byte\",-3]},{\"value\":\"motorway\",\"weight\":[\"Byte\",-2]}]
很明显特征是一个值-权重对的列表。但是代表每一对的 json 对象是什么样子的呢?我无法成功解析权重列表。
到目前为止,我已经
public enum RouteFeatureType
{
TollRoad,
motorway,
boatFerry,
railFerry,
Tunnel,
DirtRoad,
Park
}
public class RouteFeatureWeight
{
public RouteFeatureType value { get; set; }
public List<string> weight { get; set; }
}
这给了我作为字符串列表的权重 {"Byte", "-3"}.
什么是正确的类型而不是 List,所以我获得了正确的 RouteFeatureWeightType 值,定义为
public enum RouteFeatureWeightType
{
StrictExclude = -3,
SoftExclude = -2,
Avoid = -1,
Normal = 0
}
?
为什么要解析响应特征数组,因为它与您在相关请求中发送的特征相匹配?
否则,我认为您不需要保持响应特征数组不变。因此,你可以有一个解析方法如下(接近JavaScript的伪代码,你可以适配):
parseFeatures(responseFeatureArray) {
var features = [];
for (var i = 0; i < responseFeatureArray.length; i++) {
var featureObject = responseFeatureArray[i];
var featureName = featureObject.get("value");
var featureWeight = parseInt(featureObject.get("weight")[1]);
features.push(new RouteFeatureWeight(featureName, featureWeight));
}
return features;
}
让我知道进展如何。干杯。
我这样解决了问题
public class RouteFeatureWeight
{
public RouteFeatureType value { get; set; }
public List<dynamic> weight { get; set; }
public RouteFeatureWeightType FeatureWeight { get; set; }
}
反序列化响应后(使用Newtonsoft.Json)
HereResponseType response = JsonConvert.DeserializeObject<HereResponseType>(jsonResponse);
我在每个 RouteFeatureWeight 对象中将权重转换为 RouteFeatureWeightType 枚举类型
FeatureWeight = (RouteFeatureWeightType)weight[1];
根据 API 文档 (https://developer.here.com/rest-apis/documentation/routing/topics/resource-param-type-routing-mode.html),路线要素权重显示为 RouteFeatureType: RouteFeatureWeightType
对列表。
如果我想在一个calculateroute
请求中设置2个或更多权重,即
"motorway:-2,boatFerry:-3
我在 CalculateRouteResponseType
响应中得到了结果路线的相关部分:
\"feature\":
[{\"value\":\"boatFerry\",\"weight\":[\"Byte\",-3]},{\"value\":\"motorway\",\"weight\":[\"Byte\",-2]}]
很明显特征是一个值-权重对的列表。但是代表每一对的 json 对象是什么样子的呢?我无法成功解析权重列表。
到目前为止,我已经
public enum RouteFeatureType
{
TollRoad,
motorway,
boatFerry,
railFerry,
Tunnel,
DirtRoad,
Park
}
public class RouteFeatureWeight
{
public RouteFeatureType value { get; set; }
public List<string> weight { get; set; }
}
这给了我作为字符串列表的权重 {"Byte", "-3"}.
什么是正确的类型而不是 List,所以我获得了正确的 RouteFeatureWeightType 值,定义为
public enum RouteFeatureWeightType
{
StrictExclude = -3,
SoftExclude = -2,
Avoid = -1,
Normal = 0
}
?
为什么要解析响应特征数组,因为它与您在相关请求中发送的特征相匹配?
否则,我认为您不需要保持响应特征数组不变。因此,你可以有一个解析方法如下(接近JavaScript的伪代码,你可以适配):
parseFeatures(responseFeatureArray) {
var features = [];
for (var i = 0; i < responseFeatureArray.length; i++) {
var featureObject = responseFeatureArray[i];
var featureName = featureObject.get("value");
var featureWeight = parseInt(featureObject.get("weight")[1]);
features.push(new RouteFeatureWeight(featureName, featureWeight));
}
return features;
}
让我知道进展如何。干杯。
我这样解决了问题
public class RouteFeatureWeight
{
public RouteFeatureType value { get; set; }
public List<dynamic> weight { get; set; }
public RouteFeatureWeightType FeatureWeight { get; set; }
}
反序列化响应后(使用Newtonsoft.Json)
HereResponseType response = JsonConvert.DeserializeObject<HereResponseType>(jsonResponse);
我在每个 RouteFeatureWeight 对象中将权重转换为 RouteFeatureWeightType 枚举类型
FeatureWeight = (RouteFeatureWeightType)weight[1];