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

如何在Ballerina中将HTTP POST响应的字符串数组转为记录数组

在Ballerina中将HTTP响应字符串数组转换为自定义记录数组的正确方法

问题背景

已定义以下与Building相关的记录类型:

public type Coordinate record {|
    float longitude;
    float latitude;
|};

public type Campus record {|
    string code;
    string name;
|};

public type Site record {|
    string location;
|};

public type Building record {
    string code;
    string address;
    Site site;
    Campus campus;
    Coordinate coordinate;
};

尝试将JSON字符串转换为Building[]类型时失败,原实现代码如下:

service / on ln {
    isolated resource function get test() returns anydata|error?
    {
        json mockedResponse = "[{\"site\": {\"location\": \"Winterthur\"}, \"campus\": {\"code\": \"T\", \"name\": \"Technikumstrasse\"}, \"code\": \"TD\", \"address\": \"Obere Kirchgasse 2, 8500 Winterthur\", \"coordinate\": {\"longitude\": 8.729426, \"latitude\": 47.498449}}, {\"site\": {\"location\": \"Winterthur\"}, \"campus\": {\"code\": \"T\", \"name\": \"Technikumstrasse\"}, \"code\": \"TH\", \"address\": \"Hauptgeb\\u00e4ude, 8500 Winterthur\",\"coordinate\": {\"longitude\": 8.729357, \"latitude\": 47.497303}}]";
        json response = <json> check mockedResponse.cloneWithType(json);
        Building[] buildings = <Building[]> check response.cloneWithType();

        foreach Building building in buildings {
            io:print(building.address);
        }
    }
}

问题原因

原代码错误地将JSON字符串直接赋值给json类型变量,后续的cloneWithType无法正确解析字符串内容。正确流程应该是先将字符串解析为json对象,再转换为自定义记录数组。

修正方案

使用json:fromString()函数将JSON字符串解析为json类型,再通过类型转换得到Building[]数组。修正后的完整代码如下:

import ballerina/io;
import ballerina/json;

public type Coordinate record {|
    float longitude;
    float latitude;
|};

public type Campus record {|
    string code;
    string name;
|};

public type Site record {|
    string location;
|};

public type Building record {
    string code;
    string address;
    Site site;
    Campus campus;
    Coordinate coordinate;
};

service / on ln {
    isolated resource function get test() returns anydata|error?
    {
        string mockedResponseStr = "[{\"site\": {\"location\": \"Winterthur\"}, \"campus\": {\"code\": \"T\", \"name\": \"Technikumstrasse\"}, \"code\": \"TD\", \"address\": \"Obere Kirchgasse 2, 8500 Winterthur\", \"coordinate\": {\"longitude\": 8.729426, \"latitude\": 47.498449}}, {\"site\": {\"location\": \"Winterthur\"}, \"campus\": {\"code\": \"T\", \"name\": \"Technikumstrasse\"}, \"code\": \"TH\", \"address\": \"Hauptgeb\\u00e4ude, 8500 Winterthur\",\"coordinate\": {\"longitude\": 8.729357, \"latitude\": 47.497303}}]";
        
        // 1. 将JSON字符串解析为json对象
        json response = check json:fromString(mockedResponseStr);
        
        // 2. 将json对象转换为Building[]数组
        Building[] buildings = <Building[]> check response;

        foreach Building building in buildings {
            io:println(building.address);
        }
        return buildings;
    }
}

关键说明

  1. 字符串解析:json:fromString()是Ballerina中专门用于将JSON字符串解析为json类型的函数,能正确处理转义字符和数组结构。
  2. 类型转换:直接通过<Building[]> check response即可将json数组转换为自定义记录数组,无需使用cloneWithType。
  3. 错误处理:使用check关键字处理解析和转换过程中可能出现的错误(如JSON格式无效、字段不匹配等)。

内容的提问来源于stack exchange,提问作者Shammi Kolonne

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 18:42:33