Salesforce Apex 使用JSON数据的示例程序

本文介绍了一个在Salesforce Apex中使用JSON数据的示例程序, 该示例程序由以下几部分组成:

1) Album.cls, 定了了封装相关字段的数据Model类
2) RestClient.cls ,实现了一个REST服务的客户端, 将REST服务返回的JSON数据转换为Album的列表
3) AlbumController.cls ,实现了一个Salesforce的Controller, 将Album列表提供给UI页面
4) AlbumList.page ,实现了一个Salesforce的UI页面, 显示Album列表

Album.cls的实现

    public class Album {
        public Integer id  { get; set; }
        public String title  { get; set; }
    }

该Model类包含两个字段, idtitle

RestClient.cls的实现

    public class RestClient {
    
        public List<Album> getAlbums() {
            //build request
            String baseURL = 'http://jsonplaceholder.typicode.com/albums/';
            
            HttpRequest req = new HttpRequest();
            req.setEndpoint(baseURL);
            req.setMethod('GET');
                
            //call REST server
            Http http = new Http();
            HttpResponse res = http.send(req);
            String response = res.getBody();
            
            System.debug('Rest Service Response: ' + response);
                
            //Convert REST response JSON to object     
            List<Object> objects = (List<Object>) JSON.deserializeUntyped(response);
            List<Album> albums = new List<Album>();
            
            for(Object theObject : objects) {
                Map<String, Object> albumJSONObject = (Map<String, Object>) theObject;
                
                Album theAlbum = new Album();
                theAlbum.id = (Integer) albumJSONObject.get('id');
                theAlbum.title = (String) albumJSONObject.get('title');
                albums.add(theAlbum);
            }  
            return albums;
        }
    }

以上代码首先使用http://jsonplaceholder.typicode.com/albums/提供的REST服务, 该服务放回如下的JSON数据。

        [
            {
                "userId": 1,
                "id": 1,
                "title": "quidem molestiae enim"
            },
            {
                "userId": 1,
                "id": 2,
                "title": "sunt qui excepturi placeat culpa"
            },
            {
                "userId": 1,
                "id": 3,
                "title": "omnis laborum odio"
            },
            {
                "userId": 1,
                "id": 4,
                "title": "non esse culpa molestiae omnis sed optio"
            },
            {
                "userId": 1,
                "id": 5,
                "title": "eaque aut omnis a"
            },
            {
                "userId": 1,
                "id": 6,
                "title": "natus impedit quibusdam illo est"
            },
            {
                "userId": 1,
                "id": 7,
                "title": "quibusdam autem aliquid et et quia"
            },
            ...
        ]

然后使用Apex提供的JSON.deserializeUntyped方法将JSON数据转换为一个Map<String, Object>, 接着根据JSON数据结构依次构造Album对象并加入列表之中。

最后返回Album对象列表。

AlbumController.cls和AlbumList.page的实现

    public class AlbumController {
        public List<Album> albums {get;set;}
        
        public AlbumController () 
        {
            RestClient client = new RestClient();
            
            albums = new List<Album>();
            albums.addAll(client.getAlbums());
        }
    }
    <apex:page controller="AlbumController" showChat="false" showHeader="false">
        <apex:pageBlock title="Albums" >
            <apex:pageblocktable value="{!albums}" var="album">
                <apex:column headervalue="Id" value="{!album.id}"/>
                <apex:column headervalue="Title" value="{!album.title}"/>
            </apex:pageblocktable>
        </apex:pageBlock>>
    </apex:page>

AlbumControllerAlbumList的实现十分简单, AlbumController在其构造函数中调用RestClient以获取Album列表,AlbumList则使用一个pageblocktable组件来显示Album的信息。

转载于:https://www.cnblogs.com/huyouhengsf/p/6201254.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值