通过改造解析多个 json
Parse multiple json with retrofit
这是我的 json 的结构,我想使用改造从 android 客户端发送到服务器。我尝试了几种方法,但没有任何效果。在这里寻找解决方案并提供您宝贵的建议。提前谢谢
private int studentId;
private Set<Degrees> studentdegrees;
private Set<Course> coursenames;
private Set<CourseAvailability> availabilities;
下面是json
{
"studentId" : 440,
"firstName" : "nelu",
"lastName" : "wakaran",
"dateOfBirth" : "2021-02-02",
"memberSince" : "2020-01-20",
"Degrees" :[
{
"studentDegree": "xxx",
"yearOfPassing" : "1985",
"institutionName" : "xxx",
"InstitutionLocation" : "xxxx"
}
],
"course" :[
{
"registrationNumber": "xxxx",
"coursename" : "xxxx",
"stateCode" : "MH",
"dateOfregistration" : "2020-01-20",
"status" : true
}
],
"avalabilities" :[
{
"day": "Friday",
"sessionStartTime" : "4am",
"sessionEndTime" : "4am",
}
]
}
Created three pojos
Student
Degree
Course
CourseAvailability
下面是回复json
{
"message": "Student profile updated successfully",
"status": 200
}
我的改装电话
@POST("user/updateStudentProfile")
Call<StudentProfileResponse> addCourse(@Body Student student);
这就是我在改造中调用的方式
Call<StudentProfileResponse> responseCall = AppAuthClient.vuAPIServices().addCourse(student);
responseCall.enqueue(new Callback<StudentProfileResponse>() {
@Override
public void onResponse(@NotNull Call<StudentProfileResponse> call, @NotNull Response<StudentProfileResponse> response) {
if (response.isSuccessful()) {
发布更多代码后,我想我明白了问题所在。您没有为您的变量使用与预期的 json
键相同的名称。 GSON 使用您的变量名作为默认值,如果没有使用 @SerializedName
注释另外指定的话。
示例:
@SerializedName("asdf")
private int variable1;
private int variable2;
在这个例子中,variable1
有注释,所以它会覆盖名称,你应该得到一个 JSON,比如 {"asdf": 5, "variable2": 2}
。
在您的特定示例中,我认为您应该为大多数(如果不是全部)变量添加 @SerializedName
注释,以避免混淆。
@SerializedName("studentId") // not mandatory, cause they're the same
private int studentId;
@SerializedName("Degrees") // CaseSensitive, pay good attention, the JSON must match verbatim
private Set<Degrees> studentdegrees;
@SerializedName("course")
private Set<Course> coursenames;
@SerializedName("availabilities")
private Set<CourseAvailability> availabilities;
这是我的 json 的结构,我想使用改造从 android 客户端发送到服务器。我尝试了几种方法,但没有任何效果。在这里寻找解决方案并提供您宝贵的建议。提前谢谢
private int studentId;
private Set<Degrees> studentdegrees;
private Set<Course> coursenames;
private Set<CourseAvailability> availabilities;
下面是json
{
"studentId" : 440,
"firstName" : "nelu",
"lastName" : "wakaran",
"dateOfBirth" : "2021-02-02",
"memberSince" : "2020-01-20",
"Degrees" :[
{
"studentDegree": "xxx",
"yearOfPassing" : "1985",
"institutionName" : "xxx",
"InstitutionLocation" : "xxxx"
}
],
"course" :[
{
"registrationNumber": "xxxx",
"coursename" : "xxxx",
"stateCode" : "MH",
"dateOfregistration" : "2020-01-20",
"status" : true
}
],
"avalabilities" :[
{
"day": "Friday",
"sessionStartTime" : "4am",
"sessionEndTime" : "4am",
}
]
}
Created three pojos
Student
Degree
Course
CourseAvailability
下面是回复json
{
"message": "Student profile updated successfully",
"status": 200
}
我的改装电话
@POST("user/updateStudentProfile")
Call<StudentProfileResponse> addCourse(@Body Student student);
这就是我在改造中调用的方式
Call<StudentProfileResponse> responseCall = AppAuthClient.vuAPIServices().addCourse(student);
responseCall.enqueue(new Callback<StudentProfileResponse>() {
@Override
public void onResponse(@NotNull Call<StudentProfileResponse> call, @NotNull Response<StudentProfileResponse> response) {
if (response.isSuccessful()) {
发布更多代码后,我想我明白了问题所在。您没有为您的变量使用与预期的 json
键相同的名称。 GSON 使用您的变量名作为默认值,如果没有使用 @SerializedName
注释另外指定的话。
示例:
@SerializedName("asdf")
private int variable1;
private int variable2;
在这个例子中,variable1
有注释,所以它会覆盖名称,你应该得到一个 JSON,比如 {"asdf": 5, "variable2": 2}
。
在您的特定示例中,我认为您应该为大多数(如果不是全部)变量添加 @SerializedName
注释,以避免混淆。
@SerializedName("studentId") // not mandatory, cause they're the same
private int studentId;
@SerializedName("Degrees") // CaseSensitive, pay good attention, the JSON must match verbatim
private Set<Degrees> studentdegrees;
@SerializedName("course")
private Set<Course> coursenames;
@SerializedName("availabilities")
private Set<CourseAvailability> availabilities;