You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在D365 X++中反序列化Google Maps距离矩阵JSON遇阻

解决D365 X++中FormJsonSerializer反序列化Google Distance Matrix API的Elements问题

首先明确Google Distance Matrix API返回的核心JSON结构(elements为嵌套数组,包含distance对象):

{
  "rows": [
    {
      "elements": [
        {
          "distance": {
            "text": "10 km",
            "value": 10000
          },
          "status": "OK"
        }
      ]
    }
  ]
}

步骤1:创建层级匹配的数据契约类

需要按照JSON的嵌套结构,从最内层到最外层依次定义数据契约:

1.1 定义Distance数据契约

[DataContractAttribute]
class GoogleDistance extends DataContractBase
{
    str text;
    int value;

    [DataMemberAttribute('text')]
    public str parmText(str _text = text)
    {
        text = _text;
        return text;
    }

    [DataMemberAttribute('value')]
    public int parmValue(int _value = value)
    {
        value = _value;
        return value;
    }
}

1.2 定义Element数据契约

[DataContractAttribute]
class GoogleMatrixElement extends DataContractBase
{
    GoogleDistance distance;
    str status;

    [DataMemberAttribute('distance')]
    public GoogleDistance parmDistance(GoogleDistance _distance = distance)
    {
        distance = _distance;
        return distance;
    }

    [DataMemberAttribute('status')]
    public str parmStatus(str _status = status)
    {
        status = _status;
        return status;
    }
}

1.3 定义Row数据契约

[DataContractAttribute]
class GoogleMatrixRow extends DataContractBase
{
    GoogleMatrixElement elements[];

    [DataMemberAttribute('elements')]
    public GoogleMatrixElement[] parmElements(GoogleMatrixElement[] _elements = elements)
    {
        elements = _elements;
        return elements;
    }
}

1.4 定义根数据契约

[DataContractAttribute]
class GoogleDistanceMatrixResponse extends DataContractBase
{
    GoogleMatrixRow rows[];

    [DataMemberAttribute('rows')]
    public GoogleMatrixRow[] parmRows(GoogleMatrixRow[] _rows = rows)
    {
        rows = _rows;
        return rows;
    }
}

步骤2:反序列化并获取Distance值

public static void deserializeDistanceMatrix(str _jsonResponse)
{
    FormJsonSerializer serializer = new FormJsonSerializer();
    GoogleDistanceMatrixResponse response;
    GoogleMatrixRow row;
    GoogleMatrixElement element;
    GoogleDistance distance;

    try
    {
        response = serializer.deserializeObject(classNum(GoogleDistanceMatrixResponse), _jsonResponse);

        // 遍历获取第一个行的第一个元素的距离值(X++数组下标从1开始)
        if (response.parmRows() && response.parmRows()[1])
        {
            row = response.parmRows()[1];
            if (row.parmElements() && row.parmElements()[1])
            {
                element = row.parmElements()[1];
                distance = element.parmDistance();

                if (distance)
                {
                    info(strFmt("距离数值:%1,文本描述:%2", distance.parmValue(), distance.parmText()));
                }
            }
        }
    }
    catch (Exception::CLRError)
    {
        error("反序列化失败:" + CLRInterop::getLastException().ToString());
    }
}

常见排查点

  • 确保所有数据契约类继承DataContractBase,DataMemberAttribute的参数与JSON键名完全一致(大小写敏感)
  • JSON数组对应X数组类型,注意X数组下标从1开始
  • 检查API返回的status字段,若不为"OK",distance可能不存在,需做空值判断
  • 反序列化报错时,捕获CLRError查看详细异常,定位字段不匹配问题

内容的提问来源于stack exchange,提问作者Pradeep Itnal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 01:00:08