JSON.serialize不为嵌套对象返回有效的json

时间:2018-01-29 17:34:30

标签: json salesforce apex

我需要将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映射的键集。

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

但integration_checklist__c没有'attributes'节点,其字段表示如下: {"Integration_Checklist__c:{Id=a1E5E00000088lNUAQ, Name=INT-000339, etc.

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

有什么想法吗?提前致谢

2 个答案:

答案 0 :(得分:0)

嗯......您认为结果应该是什么?你将如何表示其键也是对象的地图&#34;在一个简单的javascript对象中(因为在序列化过程中你的地图键会被翻译成字段名)?

选择一个原语作为映射键(Id,Decimal,String)或最坏情况 - 将键序列化为字符串,构建新Map&gt;和序列化?

SF在这里尝试,它调用&#34; toString()&#34;在你的sobjects使用键,这就是为什么你看到一个巨大的字符串。它看起来与System.debug()调用的结果完全相同,不是吗?

答案 1 :(得分:0)

感谢@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输出。