You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Salesforce Apex调用外部API反序列化失败,对象数据为空

问题分析与解决方案

核心问题总结

导致反序列化后字段全为null的原因有以下4点:

  • JSON结构不匹配:返回的JSON根对象包含Product_Catagories数组,而非单个Product_Catagories实例,直接反序列化到单个对象会完全无法匹配数据
  • 字段大小写不匹配:Apex JSON反序列化默认区分大小写,JSON中的ProductFamily、RelatedProducts与类中定义的productFamily、relatedProducts大小写不一致,导致字段无法映射
  • 数据类型不匹配:JSON中productfeatures是数组类型,但Apex类中定义为单个POCProductfeature对象,类型不匹配导致赋值失败
  • 请求头拼写错误:Content-Type写成了application/jsson,正确应为application/json,可能引发API返回异常或格式错误

修正步骤

1. 定义外层包装类

创建匹配JSON根结构的包装类,用来承载Product_Catagories数组:

public class POCResponseWrapper {
    public List<Product_Catagories> Product_Catagories;
}

2. 修正字段匹配与类型

有两种方式解决大小写匹配问题,同时修正productfeatures的类型:

  • 方式一:直接修改Apex字段名,与JSON键名完全一致(简单直接)
    public class Product_Catagories {
        public String id;
        public String name;
        public String description;
        public String region;
        public String category; 
        public String ProductFamily; // 与JSON键名一致
        public String RelatedProducts; // 与JSON键名一致
        public List<POCProductfeature> productfeatures; // 改为数组类型
    }
    
  • 方式二:使用@JsonProperty注解指定JSON键名(适合无法修改字段名的场景)
    public class Product_Catagories {
        public String id;
        public String name;
        public String description;
        public String region;
        public String category; 
        @JsonProperty('ProductFamily')
        public String productFamily;
        @JsonProperty('RelatedProducts')
        public String relatedProducts;
        public List<POCProductfeature> productfeatures; // 改为数组类型
    }
    

3. 修正反序列化与请求逻辑

调整反序列化目标类型为包装类,同时修正请求头拼写:

private static List<Product_Catagories> getPOCProducts(){
    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/json'); // 修正拼写错误
    req.setHeader('subscription-Key','db163fc326d***');
    HttpResponse res = h.send(req);
    
    // 先反序列化为包装类,再提取数组数据
    POCResponseWrapper responseWrapper = (POCResponseWrapper)System.JSON.deserialize(res.getBody(), POCResponseWrapper.class);
    system.debug('Data: ' + responseWrapper.Product_Catagories);
    return responseWrapper.Product_Catagories;
}

4. 修正getPOCCats方法

因为getPOCProducts现在直接返回数组,无需再包装:

public static List<Product_Catagories> getPOCCats(){
    return getPOCProducts();
}

完整修正代码

public class POCGetCategories {
    
    public static List<Product_Catagories> getPOCCats(){
        return getPOCProducts();
    }
    
    // 外层包装类,匹配JSON根结构
    public class POCResponseWrapper {
        public List<Product_Catagories> Product_Catagories;
    }

    public class Product_Catagories {
        public String id;
        public String name;
        public String description;
        public String region;
        public String category; 
        public String ProductFamily; // 与JSON键名一致
        public String RelatedProducts; // 与JSON键名一致
        public List<POCProductfeature> productfeatures; // 改为数组类型
    }

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

    private static List<Product_Catagories> getPOCProducts(){
        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/json'); // 修正拼写错误
        req.setHeader('subscription-Key','db163fc326d***');
        HttpResponse res = h.send(req);
        
        POCResponseWrapper responseWrapper = (POCResponseWrapper)System.JSON.deserialize(res.getBody(), POCResponseWrapper.class);
        system.debug('Data: ' + responseWrapper.Product_Catagories);
        return responseWrapper.Product_Catagories;
    }
}

内容的提问来源于stack exchange,提问作者Hasan Zubairi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 04:55:28