前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Salesforce Apex Rest Callout 取得JSON形式的数据

Salesforce Apex Rest Callout 取得JSON形式的数据

原创
作者头像
repick
发布2023-11-06 21:14:01
1700
发布2023-11-06 21:14:01
举报
文章被收录于专栏:SalesforceSalesforce

以下边网站为例,用浏览器打开URL:https://th-apex-http-callout.herokuapp.com/animals,会返回JSON形式数据

代码语言:javascript
复制
{
    "animals": [
        "majestic badger",
        "fluffy bunny",
        "scary bear",
        "chicken"
    ]
}

1.把URL添加到Salesforce中

2.ApexClass中实现Service中取得JSON形式的数据。

callOutSample.cls

代码语言:javascript
复制
public with sharing class callOutSample {
    public static Map<String, Object> getCalloutInfo() {
        Map<String, Object> results = new Map<String, Object>();
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        System.debug('>>>debuglog>>>>>>>>>>response>>>>>>>>>>>>>'+response);
        // If the request is successful, parse the JSON response.
        if(response.getStatusCode() == 200) {
            // Deserialize the JSON string into collections of primitive data types.
            results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
        }
        return results;
    }
}

3.调用上边做成的Apex方法,查看结果

callOutApex.cls

代码语言:javascript
复制
public with sharing class callOutApex {
    public callOutApex() {
        Map<String, Object> results = callOutSample.getCalloutInfo();
        // // Cast the values in the 'animals' key as a list
        List<Object> animals = (List<Object>) results.get('animals');
        System.debug('Received the following animals:');
        for(Object animal: animals) {
            System.debug(animal);
        }
    }
}

4.匿名块执行

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.把URL添加到Salesforce中
  • 2.ApexClass中实现Service中取得JSON形式的数据。
  • 3.调用上边做成的Apex方法,查看结果
  • 4.匿名块执行
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档