java Salesforce外部API未反序列化

6ljaweal  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(118)

我对salesforce和apex编程比较陌生。最近我被指派从外部API获取数据。我正在接收一个对象,该对象在主对象内部也有另一个对象。我应该接收的数据如下

{
    "Product_Catagories": [{
        "id": "8ad08aef8534de220185400383d82def",
        "name": "Product One",
        "description": "Desc One",
        "region": "",
        "category": "Main Products",
        "ProductFamily": "Main",
        "RelatedProducts": "POC-B0000001",
        "productfeatures": []
    }, {
        "id": "8ad0887e8534de2701853fff5a9b22ee",
        "name": "Product Two",
        "description": "Desc Two",
        "region": "",
        "category": "Main Products",
        "ProductFamily": "Main",
        "RelatedProducts": "POC-B0000002",
        "productfeatures": []
    }, {
        "id": "8ad08aef8534de2201853ffe48fc08f6",
        "name": "Product Three",
        "description": "Desc Three",
        "region": "",
        "category": "Main Products",
        "ProductFamily": "Main",
        "RelatedProducts": "POC-B0000003",
        "productfeatures": []
    }]
}

但我越来越
Product_Catagories:[category=null, description=null, id=null, name=null, productFamily=null, productfeatures=null, region=null, relatedProducts=null]
我已经在apex代码中定义了类,但是对象是空的。没有数据显示。我使用的代码如下

public class POCGetCategories {
    
    public static List<Product_Catagories> getPOCCats(){
        
        return new List<Product_Catagories>{getPOCProducts()};
        
    }
    
    public class Product_Catagories {
        public String id;
        public String name;
        public String description;
        public String region;
        public String category; 
        public String productFamily;
        public String relatedProducts;
        public POCProductfeature productfeatures;
    }

    public class POCProductfeature {
        public String id;
        public String name;
        public String code;
        public String status;
        public String description;
    }

    private static Product_Catagories getPOCProducts(){
            
            Product_Catagories cats = new Product_Catagories();
            Http h = new Http();
                String method = 'GET';
                String url = 'https://externalapi';
            HttpRequest req = new HttpRequest();

                req.setEndpoint(url);
                req.setMethod(method);      
                req.setTimeout(120000);
                req.setHeader('Content-Type', 'application/jsson');
                req.setHeader('subscription-Key','db163fc326d***');
                HttpResponse res = h.send(req);
            cats = (Product_Catagories)System.JSON.deserialize(res.getBody(), Product_Catagories.class);
            system.debug('Data: ' + cats);
            return cats;
    }
}
2ul0zpep

2ul0zpep1#

主响应对象是一个具有“Product_Catagories”键的对象,其中包含一个单独类别的数组。每个类别中的功能键也是一个列表。您的模型应如下所示:

public class ProductCatagoriesResponse {
    public List<Product_Catagory> Product_Catagories;
}

public class Product_Catagory {
    public String id;
    public String name;
    public String description;
    public String region;
    public String category; 
    public String productFamily;
    public String relatedProducts;
    public List<POCProductfeature> productfeatures;
}

...

ProductCatagoriesResponse cats = (ProductCatagoriesResponse)System.JSON.deserialize(res.getBody(), ProductCatagoriesResponse.class);

还有,它的拼写是“类别”。但我假设这是他们的错别字,你只需要处理它...

相关问题