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

Power Apps自定义连接器JSON解析错误:预期对象却得到字符串

问题根源

返回的响应里,Data字段的值是转义后的JSON字符串,不是直接的嵌套JSON对象。模型驱动应用的JSON解析器要求该字段必须是对象类型,所以抛出了"expected 'object' but got 'string'"的错误;而画布应用可能做了隐式的自动解析,所以能正常工作。

修复方案

方案1:修改后端服务(推荐)

问题出在后端返回响应的逻辑:你把ExpandoObject序列化成JSON字符串后,又把这个字符串当成外层响应的Data值,导致外层Data变成了字符串类型。直接改后端代码,返回嵌套对象即可:

try
{
    Log.Debug($@"X-Correlation-ID {CorrelationId} - START: GetMultipleFileInfo- ids {ids}");
    List<MsdDataCollectionFile> fileinfo = GetMultipleFileInfoData(ids);
    // 直接构建嵌套结构的响应对象,避免二次序列化
    var responseObj = new 
    {
        User = "55205bb2-28c4-ed11-83ff-0022489ede2f", // 替换为实际用户ID的获取逻辑
        ErrorMessage = (string)null,
        Data = new 
        {
            TotalRecordCount = fileinfo.Count,
            TotalRecordExcluded = fileinfo.Where(x => x.Excluded).Count(),
            Data = fileinfo
        }
    };
    Log.Debug($@"X-Correlation-ID {CorrelationId} - END: GetMultipleFileInfoData- totalrecord {fileinfo.Count}");
    // 返回对象,让框架自动完成整个响应的JSON序列化
    return new HttpResponse(responseObj, null);
}

修改后返回的响应会变成正确的嵌套结构:

{
    "User": "55205bb2-28c4-ed11-83ff-0022489ede2f",
    "ErrorMessage": null,
    "Data": {
        "TotalRecordCount":1,
        "TotalRecordExcluded":0,
        "Data":[{
            "Id":"13a0293d-c7de-ed11-a7c6-0022489ede2f",
            "Name":"Second trial!",
            "FileName":"Another File Name",
            "TotalRows":14,
            "IsProcessed":false,
            "Date":"2023-04-17T22:00:00Z",
            "ExpectedFileDate":"2023-04-03T22:00:00Z",
            "IsCompleted":false,
            "ErrorMessages":null,
            "Excluded":false,
            "LogicalName":"eps_datacollectionfile"
        }]
    }
}

方案2:在自定义连接器中添加解析逻辑(无法改后端时用)

如果没法修改后端,就在自定义连接器里加一步,把Data字段的字符串解析成对象:

  1. 打开自定义连接器的编辑页面
  2. 找到响应定义或者添加操作后脚本
  3. 加入以下脚本完成字符串转对象:
var response = JSON.parse(context.response);
response.Data = JSON.parse(response.Data);
context.response = JSON.stringify(response);

验证修改

改完之后,不管是画布应用还是模型驱动应用,都可以直接用原来的代码访问数据:

ClearCollect(connectorCollection, DataCollection.GetFile(userId, "application/json", {body:"13a0293d-c7de-ed11-a7c6-0022489ede2f"}).Data.Data)

内容的提问来源于stack exchange,提问作者Alessandro Marchesin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 14:52:39