我尝试使用改造将此数据发布到服务器。 所以我尝试创建序列化请求来发布这些数据。 通常如果这个请求喜欢
{
"FirstName":"test",
"LastName":"test",
"EmailAddress":"test@test.com",
"Password":"test",
"DeviceToken":"",
"PushToken":"",
"DeviceType":"test",
"SMSSubscribe":"1",
"EmailSubscribe":"1",
"TermsCondition":"1",
}
然后我像这样创建了Serialized类
public class RegisterRequest {
@SerializedName("DeviceToken")
public String deviceToken;
@SerializedName("PushToken")
public String pushToken;
@SerializedName("AppVersion")
public String appVersion;
@SerializedName("DevicePlatform")
public String devicePlatform;
@SerializedName("DeviceType")
public String deviceType;
@SerializedName("FirstName")
public String firstName;
@SerializedName("LastName")
public String lastName;
@SerializedName("EmailAddress")
public String email;
@SerializedName("Password")
public String password;
@SerializedName("MobileCountryCode")
public String mobileCountryCode;
@SerializedName("MobileNumber")
public String mobileNumber;
@SerializedName("SMSSubscribe")
public String smsSubscribe;
@SerializedName("EmailSubscribe")
public String emailSubscribe;
@SerializedName("TermsCondition")
public String termsCondition;
@SerializedName("DeviceInformation")
public String deviceInformation;
} 那么
@Headers({TOKEN, CONTENT_TYPE_JSON, LANGUAGE_CODE, APP_VERSION})
@POST("api/RegisterStep1")
Call<CommonResponse> register(@Body RegisterRequest registerRequest);
然后我发布这样的数据
final RegisterRequest registerRequest = new RegisterRequest();
registerRequest.firstName = givenName;
registerRequest.lastName = familyName;
registerRequest.email = email;
registerRequest.password = password;
registerRequest.deviceToken = Repository.getDeviceId();
registerRequest.pushToken = "";
registerRequest.deviceType = Repository.DEVICE_TYPE;
registerRequest.appVersion = Repository.APP_VERSION;
registerRequest.devicePlatform = Repository.DEVICE_PLATFORM;
registerRequest.mobileNumber = mobileNo;
registerRequest.mobileCountryCode = mSelectedCountryCode;
registerRequest.deviceInformation = Repository.DEVICE_INFO;
Repository.apiService().register(registerRequest).enqueue(new BaseCallback<CommonResponse>() {
@Override
public void onSuccess(CommonResponse body) {
super.onSuccess(body);
但现在我想知道如何创建Serialized类以将此格式数据发布到服务器
{
"REG_Company": {
"ReferenceCode":"ReferenceCode",
"CompanyType":"1",
"CompanyName": "Test",
"CompanyEmailAddress": "test@test.com",
"CompanyTier": "1",
"ContactNumber": "99999",
"OilChangePerMonth": "1",
"OilChangePercentage": "1",
"ShellHelixQty": "1",
"Remarks": "1"
},
"REG_CompanyAddress": {
"AddressType":"1",
"Address1":"Address1",
"Address2":"Address2",
"StreetName":"StreetName",
"CompanyCity":"CompanyCity",
"CompanyState":"CompanyState",
"PostalCode":"PostalCode",
"Longitude":"Longitude",
"Latitude":"Latitude"
},
"REG_CompanyService": [
{"ServiceID":1
},{"ServiceID":2}]
,
"REG_CompanyHours": [
{"WeekDay":1,
"IsOpen":1,
"StartTime":1,
"CloseTime":1
}, {"WeekDay":1,
"IsOpen":1,
"StartTime":1,
"CloseTime":1
}],
"REG_Member": {
"UserID":"UserID",
"UserPassword":"UserPassword",
"Salutation": "1",
"FirstName": "FirstName",
"LastName": "LastName",
"MobileNumber": "MobileNumber",
"EmailAddress": "EmailAddress",
"Gender": "1",
"ContactPreference": "1",
"RegistrationType": "1",
"RegistrationMode": "1"
}
}
答案 0 :(得分:1)
您可以将以下代码用作序列化模型类。
@SerializedName("REG_CompanyAddress")
public CompanyAddress address = new CompanyAddress();
@SerializedName("REG_CompanyService")
public List<Service> services = new ArrayList<>();
public static class Service {
@SerializedName("ServiceID")
public Integer serviceID;
}
public static class CompanyAddress {
@SerializedName("AddressType")
public String addressType;
@SerializedName("Address1")
public String address1;
.......
Your code
}
像这样你可以创建其他领域
答案 1 :(得分:1)
为JSON序列化手动定义POJO是一项繁琐的任务,可以(至少部分地)使用jsonschema2pojo自动化。对于您的JSON,它会生成此输出:
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("REG_Company")
@Expose
public REGCompany rEGCompany;
@SerializedName("REG_CompanyAddress")
@Expose
public REGCompanyAddress rEGCompanyAddress;
@SerializedName("REG_CompanyService")
@Expose
public List<REGCompanyService> rEGCompanyService = null;
@SerializedName("REG_CompanyHours")
@Expose
public List<REGCompanyHour> rEGCompanyHours = null;
@SerializedName("REG_Member")
@Expose
public REGMember rEGMember;
}
-----------------------------------com.example.REGCompany.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class REGCompany {
@SerializedName("ReferenceCode")
@Expose
public String referenceCode;
@SerializedName("CompanyType")
@Expose
public String companyType;
@SerializedName("CompanyName")
@Expose
public String companyName;
@SerializedName("CompanyEmailAddress")
@Expose
public String companyEmailAddress;
@SerializedName("CompanyTier")
@Expose
public String companyTier;
@SerializedName("ContactNumber")
@Expose
public String contactNumber;
@SerializedName("OilChangePerMonth")
@Expose
public String oilChangePerMonth;
@SerializedName("OilChangePercentage")
@Expose
public String oilChangePercentage;
@SerializedName("ShellHelixQty")
@Expose
public String shellHelixQty;
@SerializedName("Remarks")
@Expose
public String remarks;
}
-----------------------------------com.example.REGCompanyAddress.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class REGCompanyAddress {
@SerializedName("AddressType")
@Expose
public String addressType;
@SerializedName("Address1")
@Expose
public String address1;
@SerializedName("Address2")
@Expose
public String address2;
@SerializedName("StreetName")
@Expose
public String streetName;
@SerializedName("CompanyCity")
@Expose
public String companyCity;
@SerializedName("CompanyState")
@Expose
public String companyState;
@SerializedName("PostalCode")
@Expose
public String postalCode;
@SerializedName("Longitude")
@Expose
public String longitude;
@SerializedName("Latitude")
@Expose
public String latitude;
}
-----------------------------------com.example.REGCompanyHour.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class REGCompanyHour {
@SerializedName("WeekDay")
@Expose
public Integer weekDay;
@SerializedName("IsOpen")
@Expose
public Integer isOpen;
@SerializedName("StartTime")
@Expose
public Integer startTime;
@SerializedName("CloseTime")
@Expose
public Integer closeTime;
}
-----------------------------------com.example.REGCompanyService.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class REGCompanyService {
@SerializedName("ServiceID")
@Expose
public Integer serviceID;
}
-----------------------------------com.example.REGMember.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class REGMember {
@SerializedName("UserID")
@Expose
public String userID;
@SerializedName("UserPassword")
@Expose
public String userPassword;
@SerializedName("Salutation")
@Expose
public String salutation;
@SerializedName("FirstName")
@Expose
public String firstName;
@SerializedName("LastName")
@Expose
public String lastName;
@SerializedName("MobileNumber")
@Expose
public String mobileNumber;
@SerializedName("EmailAddress")
@Expose
public String emailAddress;
@SerializedName("Gender")
@Expose
public String gender;
@SerializedName("ContactPreference")
@Expose
public String contactPreference;
@SerializedName("RegistrationType")
@Expose
public String registrationType;
@SerializedName("RegistrationMode")
@Expose
public String registrationMode;
}
当然,您可以进一步重构生成的代码,例如:通过使主要引用的类内部静态。
答案 2 :(得分:0)
您应该使用内部类和ArrayLists。例如,如果您有:
{
"A": 1,
"B": {
"C": "d"
}
"D": [
1,
2]
}
你将拥有:
public class Model {
private int A;
private B B;
private ArrayList<Integer> D;
// Getters and setters
public class B {
private String C;
// Getters and setters
}