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的信息。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM