如何对sObject进行字符串化

时间:2015-01-29 23:19:00

标签: salesforce apex

我有一个sObjects列表,我想将所有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);
}

但这只返回“ID”和“Contact__c”。该字符串获取“Name”或“Rule_Class_c”字段。

如何获取其余数据?

1 个答案:

答案 0 :(得分:4)

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);
}