【问题标题】:JSON deserialize if the Field is missing in the JSON object , Set the Field in a wrapper class to NULL如果 JSON 对象中缺少字段,则 JSON 反序列化,将包装类中的字段设置为 NULL
【发布时间】:2020-02-14 18:52:10
【问题描述】:
JSON Object 
 {
    x = 'String1'

}

如果 JSON 对象仅传递一个值,请考虑这种情况,我想将另一个值设置为 NULL,在这种情况下是 Y

 public with sharing class SerializeObject{
        public String x {get;set;}
        public String y {get;set;}
 }

序列化函数

 public SerializeObject SerializeObjectJSON(String json){
    return (SerializeObject ) System.JSON.deserialize(json.replace('""','null'), SerializeObject.class);
  }

【问题讨论】:

    标签: json api serialization salesforce apex


    【解决方案1】:

    您的“对象”不是有效的 JSON。我不知道您要做什么,但您在数据源方面存在一些严重问题,而不是 Apex。如果您修复语法错误 - 其他变量将立即为空。

    public with sharing class SerializeObject{
            public String x {get;set;}
            public String y {get;set;}
    }
    
    String s = '{"x" : "String 1"}';
    
    SerializeObject so = (SerializeObject) JSON.deserialize(s, SerializeObject.class);
    System.debug(so.x); // String 1
    System.debug(so.y); // null
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      • 2021-09-09
      • 2012-07-04
      • 1970-01-01
      相关资源
      最近更新 更多