【发布时间】:2021-05-27 02:40:06
【问题描述】:
我的教授希望我写一个关于如何部署 Ballerina 服务的小教程。所以我正在努力学习它。我正在使用 1.2 版,我对污点检查的概念和变量类型有点不知所措...
我正在尝试编写一个最小的 REST 服务,其端点从另一个 api 请求 json 数据,然后使用该 JSON 来做事。
到目前为止的工作如下:
service tutorial on new http:Listener(9090) {
// Resource functions are invoked with the HTTP caller and the incoming request as arguments.
resource function getName(http:Caller caller, http:Request req) {
http:Client clientEP = new("https://api.scryfall.com/");
var resp = clientEP->get("/cards/random");
if (resp is http:Response) {
var payload = resp.getJsonPayload();
if (payload is json) {
var result = caller->respond(<@untainted>(payload));
} else {
log:printError("");
}
} else {
log:printError("");
}
}
使用从https://api.scryfall.com/cards/random 返回的 JSON 进行响应
但是现在让我们说,我想从该 JSON 中访问单个值。例如。 “姓名”。 如果我尝试这样访问它:payload["name"]
我得到:无效操作:类型 'json' 不支持索引
我刚刚发现,如果我先像这样创建地图,它会起作用:
map mp =
如果我然后访问 mp["name"] 它可以工作。但为什么?如果您仍然必须创建地图然后投射有效负载,那么 json 类型有什么好处?我将如何访问 json 中的 json?比如mp["data"][0]...无效操作:type 'json'不支持再次索引...
我还在尝试理解污点检查的概念...... 检查内容后,我是否只是将所有被污染的内容都投射到 上? 有时我真的不明白文档试图告诉我什么......
【问题讨论】: