我想将数据序列化为以下格式。这样我就可以从Salesforce向内部API发送呼出请求。
{
"Id": 1,
"FirstName": "sample string 2",
"LastName": "sample string 3",
"Email": "sample string 4",
"Phone": "sample string 5",
"AddressId": 6,
"Address": {
"Id": 1,
"Street1": "sample string 2",
"Street2": "sample string 3",
"StreetNo": "sample string 4",
"Zip": "sample string 5",
"City": "sample string 6",
"StateId": 7,
"State": {
"StateId": 1,
"Code": "sample string 2",
"Name": "sample string 3",
"CreatedDate": "2018-07-18T11:29:57.0831407+05:30",
"ModfiedDate": "2018-07-18T11:29:57.0831407+05:30"
},
"Phone": "sample string 8",
"Email": "sample string 9",
"WebsiteLink": "sample string 10",
"CountryId": 11,
}
我正在尝试以下代码。我需要将地址作为整个对象中的一个对象:
Address addr=new Address();
addr.Phone='8943023308';
addr.Street1='doddakanali';
addr.Street2='a144F000000Cvit';
addr.Zip=894309;
addr.City='calicut';
addr.StateId=1;
addr.CountryId=1;
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeNumberField('Id ',0);
gen.writeStringField('FirstName','leo');
gen.writeStringField('LastName','messi');
gen.writeStringField('Email','leo2016@gmail.com');
gen.writeNumberField('CompnayId ',1);
gen.writeObjectField('Address',addr);
gen.writeEndObject();
String jsonS = gen.getAsString();
System.debug('jsonMaterials'+jsonS);
答案 0 :(得分:0)
我的方法是:
1。,创建一个新的Apex类:
public class MyDataStructure {
public Address Address ;
public Decimal Id ;
public String FirstName ;
public String LastName ;
public String Email ;
public String Phone ;
public Decimal AddressId ;
public class Address {
public State State ;
public Decimal Id ;
public String Street1 ;
public String Street2 ;
public String StreetNo ;
public String Zip ;
public String City ;
public Decimal StateId ;
public String Phone ;
public String Email ;
public String WebsiteLink ;
public Decimal CountryId ;
}
public class State {
public Decimal StateId ;
public String Code ;
public String Name ;
public String CreatedDate ;
public String ModfiedDate ;
}
}
2。,构造一个或多个类,分配值,并使用JSON.serialize()
方法生成字符串:
MyDataStructure mds = new MyDataStructure();
mds.Id = 1 ;
mds.FirstName = 'sample string 2' ;
mds.LastName = 'sample string 3' ;
mds.Email = 'sample string 4' ;
mds.Phone = 'sample string 5' ;
mds.AddressId = 6 ;
mds.Address = new MyDataStructure.Address();
mds.Address.Id = 1 ;
mds.Address.Street1 = 'sample string 2' ;
mds.Address.Street2 = 'sample string 3' ;
mds.Address.StreetNo = 'sample string 4' ;
mds.Address.Zip = 'sample string 5' ;
mds.Address.City = 'sample string 6' ;
mds.Address.StateId = 7 ;
mds.Address.Phone = 'sample string 8' ;
mds.Address.Email = 'sample string 9' ;
mds.Address.WebsiteLink = 'sample string 10' ;
mds.Address.CountryId = 11 ;
mds.Address.State = new MyDataStructure.State();
mds.Address.State.StateId = 1 ;
mds.Address.State.Code = 'sample string 2' ;
mds.Address.State.Name = 'sample string 3' ;
mds.Address.State.CreatedDate = '2018-07-18T11:29:57.0831407+05:30' ;
mds.Address.State.ModfiedDate = '2018-07-18T11:29:57.0831407+05:30' ;
System.debug( JSON.serialize( mds ) );