我正在使用以下代码生成一些JSON输出:
generator = JSON.createGenerator(true);
generator.writeStartObject();
generator.writeFieldName('employees');
generator.writeStartArray();
for(User u : getUsers()) {
generator.writeStartObject();
generator.writeStringField('key', u.Id);
generator.writeStringField('nickname', u.CommunityNickname);
generator.writeStringField('lastname', u.LastName);
if(u.Region__c != null) {
generator.writeStringField('region', u.Region__c);
}
else {
generator.writeStringField('region', '');
}
if(u.Home_District__c != null) {
generator.writeStringField('district', u.Home_District__c);
}
else {
generator.writeStringField('district', '');
}
if(u.Home_District__c != null) {
generator.writeStringField('pack', u.Pack__c);
}
else {
generator.writeStringField('pack', '');
}
generator.writeStringField('employee', u.Name);
generator.writeEndObject();
}
generator.writeEndArray();
generator.writeFieldName('events');
generator.writeStartArray();
for(Site_Visit__c sv : getSiteVisits()) {
generator.writeStartObject();
if(sv.Status__c.toLowercase() == 'scheduled') {
generator.writeFieldName('assigned');
generator.writeStartObject();
if(sv.Intended_Start_Date_Time__c != null) generator.writeStringField('start_date', String.valueOf(sv.Intended_Start_Date_Time__c));
if(sv.Estimated_Completion_Date_Time__c != null) generator.writeStringField('end_date', String.valueOf(sv.Estimated_Completion_Date_Time__c));
generator.writeStringField('text', String.valueOf(sv.Name));
if(sv.Assigned_Employee__c != null) generator.writeStringField('section_id', String.valueOf(sv.Assigned_Employee__c));
generator.writeStringField('id', String.valueOf(sv.Id));
generator.writeStringField('color', String.valueOf('#ffffff'));
generator.writeStringField('textColor', String.valueOf('#000000'));
generator.writeEndObject();
}
if(sv.Status__c.toLowercase() == 'unassigned') {
generator.writeFieldName('unassigned');
generator.writeStartObject();
if(sv.Intended_Start_Date_Time__c != null) generator.writeStringField('start_date', String.valueOf(sv.Intended_Start_Date_Time__c));
if(sv.Estimated_Completion_Date_Time__c != null) generator.writeStringField('end_date', String.valueOf(sv.Estimated_Completion_Date_Time__c));
generator.writeStringField('text', String.valueOf(sv.Name));
generator.writeStringField('id', String.valueOf(sv.Id));
generator.writeStringField('color', String.valueOf('#ffffff'));
generator.writeStringField('textColor', String.valueOf('#000000'));
generator.writeStringField('woNumber', String.valueOf(sv.Work_Order__r.Name));
generator.writeStringField('woType', String.valueOf(sv.Work_Order__r.Work_Order_Type__c));
generator.writeStringField('customer', String.valueOf(sv.Work_Order__r.Customer__c));
generator.writeEndObject();
}
generator.writeEndObject();
}
generator.writeEndArray();
generator.writeEndObject();
当前输出如下:
{
employees:
employee
employee
events:
unassigned:
event
unassigned:
event
assigned:
event
assigned:
event
(empty object?)
(empty object?)
}
我需要它像这样:
{
employees:
employee
employee
events:
unassigned:
event
event
assigned:
event
event
}
事件是一个对象(不是数组),它有两个字段:已分配和未分配。每个字段都是与其描述匹配的事件数组。
感谢您的帮助。
答案 0 :(得分:0)
查看您的代码段时,如果您只是使用serialize()
method,我就不明白为什么需要在顶点使用JSON生成器。
由于您正在使用多个对象,我将创建一个自定义内部类并构建一个列表并对其进行序列化。
public MyInnerClass(User u, Site_Visit__c s){
String userName = u.Name;
String siteName = s.Name;
}
List<MyOuterClass.MyInnerClass> customInnerClass = new List<MyOuterClass.MyInnerClass>();
//add my inner class objects made up of properties from all my sobjects
customInnerClass.serialize();
//This will be a well formed JSON object for any API or JS Framework.