如何创建一个接受带有数组的 json 的 Web 服务?

问题描述 投票:0回答:1

我已经知道“官方”方式是 BATCH 请求,但根据项目规范,我需要 API 能够读取 json 数组。

这是要发送到API的数据:

[
    {
        "Color": "GENERAL",
        "Code": "112",
        "Description": "BERMELLON",
        "RGB": "255,0,0",
        "GroupWeb": "40"
    },
    {
        "Color": "GENERAL",
        "Code": "111",
        "Description": "ROJERAS",
        "RGB": "255,0,0",
        "GroupWeb": "40"
    }
]

我正在尝试使用代码单元“JSON Management”读取JSON:

codeunit 60201 IntegracionPLM
{ 
    procedure ReadColorJSON(data: Text)
    var
        JSONManagement: Codeunit "JSON Management";
        ArrayJSONManagement: Codeunit "JSON Management";
        ObjectJSONManagement: Codeunit "JSON Management";
        i: Integer;
        JsonArrayText: Text;
        ColorJsonObject: Text;
        GrupoColoresText: Text;
        CodigoText: Text;
        DescripcionText: Text;
        ValorRGBText: Text;
        AgrupacionWebText: Text;
    begin
        JSONManagement.InitializeObject(Data);

        if JSONManagement.GetArrayPropertyValueAsStringByName('data', JsonArrayText) then begin
            ArrayJSONManagement.InitializeCollection(JsonArrayText);

            for i := 0 to ArrayJSONManagement.GetCollectionCount() - 1 do begin
                ArrayJSONManagement.GetObjectFromCollectionByIndex(ColorJsonObject, i);
                ObjectJSONManagement.InitializeObject(ColorJsonObject);

                ObjectJSONManagement.GetStringPropertyValueByName('GrupoColores', GrupoColoresText);
                ObjectJSONManagement.GetStringPropertyValueByName('Codigo', CodigoText);
                ObjectJSONManagement.GetStringPropertyValueByName('Descripcion', DescripcionText);
                ObjectJSONManagement.GetStringPropertyValueByName('ValorRGB', ValorRGBText);
                ObjectJSONManagement.GetStringPropertyValueByName('AgrupacionWeb', AgrupacionWebText);

                Message('GrupoColores: %1, Codigo: %2, Descripcion: %3, ValorRGB: %4, AgrupacionWeb: %5', 
                         GrupoColoresText, CodigoText, DescripcionText, ValorRGBText, AgrupacionWebText);
            end;
        end;
    end;
}

但我收到此错误:

{
    "error": {
        "code": "BadRequest",
        "message": "One or more errors occurred. (One or more errors occurred. (An unexpected 'StartArray' node was found when reading from the JSON reader. A 'StartObject' node was expected.))  CorrelationId:  829669bb-ea16-4703-9ef1-e9431d47928e."
    }
}

尝试用不同的主体执行请求:

{
    "data": 
    [
        {
            "Color": "GENERAL",
            "Code": "112",
            "Description": "BERMELLON",
            "RGB": "255,0,0",
            "GroupWeb": "40"
        },
        {
            "Color": "GENERAL",
            "Code": "111",
            "Description": "ROJERAS",
            "RGB": "255,0,0",
            "GroupWeb": "40"
        }
    ]
}

出现其他错误:

{
    "error": {
        "code": "BadRequest",
        "message": "One or more errors occurred. (An unexpected 'StartArray' node was found when reading from the JSON reader. A 'PrimitiveValue' node was expected.)  CorrelationId:  90b8e8d6-805d-44ed-9964-6204a0b43422."
    }
}

如何从 Business Central Web 服务读取 json 数组?

microsoft-dynamics dynamics-365 navision dynamics-nav businesscentral
1个回答
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.