如何将 sObject 字符串化

How to Stringify a sObject

我有一个 sObject 列表,我想将所有 sObject 字段转换为一个字符串。例如(这是 SOQL 查询的打印输出),

Custom_sobj__c{
  "serId": 5,
  "value": {
    "Id": "a0FJ0000005zIbwMAE",
    "Contact__r": {
      "serId": 6,
      "value": {
        "Name": "Bob Bobenson",
        "Owner": {
          "serRefId": 4
        },
        "Rule_Class__c": "Class III - Quote\/De (2 more) ...",
        "OwnerId": "005d000000450RiAAI",
        "Id": "003J0000016ZjuCIAS"
      }
    },
    "Contact__c": "003J0000016ZjuCIAS"
  }
}

我有这些对象的列表。我想将其全部转换为字符串,因此它看起来像这样:

...'sobjInstance3{"serid";5,"value":...}','sobjInstance4{"serid";5,"value":...}',...

我有一个 for 循环遍历列表,

String strSobjects = ' ';   

for(Custom_sobj__c obj : sobjList){
    strSobjects = strSobjects  + ','+String.valueOf(obj);
}

但这只是 returns "ID" 和 "Contact__c"。该字符串正在获取 "Name" 或 "Rule_Class_c" 字段。

如何获取其余数据?

System.JSON 方法应该可以解决问题:https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_json_overview.htm

类似于:

String strSobjects = ' ';   

for(Custom_sobj__c obj : sobjList){
    strSobjects = strSobjects  + ','+JSON.Serialize(obj);
}