【问题标题】:JSON.serialize not returning valid json for nested objectsJSON.serialize 没有为嵌套对象返回有效的 json
【发布时间】:2018-01-29 17:34:29
【问题描述】:

我需要将 Salesforce 对象的嵌套结构序列化为 JSON,但结果解析不正确。

public class Checklist_JSON {
// class to represent the structure of the checklist 
//   one opportunity
//   one account
//   one finance checklist (custom object)
//   one/more integration checklists, (custom objects) each with
//   one/more campaigns (custom objects)
public opportunity this_opp{get;set;}
public account this_acc{get;set;}
public finance_checklist__c this_fin{get;set;}
public map <integration_checklist__c, list<ph_campaign__c>> ints_cams{get;set;}
}

然后我只是JSON.serialize(this_checklist_json);

可以正确解析除 integration_checklist__c 之外的所有对象的结果 - 即 ints_cams 映射的键集。

其他对象(帐户/oppty/等)每个都有一个节点“属性”,其中包含正确格式的名称/值对,例如 {"attributes":{"type":"Account","url":"/services/data/v42.0/sobjects/Account/0015E00000WDO8ZQAX"},"Id":"0015E00000WDO8ZQAX", etc.

但 integration_checklist__c 没有“属性”节点,并且字段表示如下: {"Integration_Checklist__c:{Id=a1E5E00000088lNUAQ, Name=INT-000339, etc.

即“ID”周围没有引号:“a1E5E00000088lNUAQ”

有什么想法吗?提前致谢

【问题讨论】:

    标签: json salesforce apex


    【解决方案1】:

    嗯...您认为结果应该是什么?您将如何在一个简单的 javascript 对象中表示“其键也是对象的映射”(因为您的映射键将在序列化过程中被转换为字段名称)?

    选择一个原语作为映射键(Id、Decimal、String)或最坏的情况 - 将键序列化为字符串,构建新 Map> 并将其序列化?

    SF 在这里尝试,它在您的 sobjects 使用的键上调用“toString()”,这就是为什么您会看到 1 个巨大的字符串。它看起来与 System.debug() 调用的结果相同,不是吗?

    【讨论】:

      【解决方案2】:

      感谢@eyescream 抽出时间回复。

      根本问题是我不应该尝试将父子关系表示为地图。

      我已经重写基于将这种关系表示为一个类:

      public class Checklist_JSON {
          // class to represent the structure of the checklist 
          //   one opportunity
          //   one account
          //   one finance checklist
          //   one/more integration checklists, each with
          //   one/more campaigns
          public opportunity this_opp{get;set;}
          public account this_acc{get;set;}
          public finance_checklist__c this_fin{get;set;}
          public list<int_cams> these_ints_cams {get;set;}
      }
      
      public class int_cams {
          //class to represent parent-children relationship int-list<cams>
          public integration_checklist__c this_int {get;set;}
          public list <ph_campaign__c> these_cams {get;set;}
      }
      

      序列化结果现在会产生我想要的 json 输出。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-17
        • 1970-01-01
        • 1970-01-01
        • 2019-12-26
        • 2020-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多