如何用json解析路由功能权重响应?

时间:2015-01-09 15:15:17

标签: c# json here-api

根据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
}

2 个答案:

答案 0 :(得分: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;
}

让我知道它是怎么回事。欢呼声。

答案 1 :(得分:0)

我用这种方式解决了问题

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];