Json解析了看起来不错的字符串上的错误(预期为BEGIN_OBJECT,但为BOOLEAN)

时间:2018-07-19 19:09:32

标签: java json gson deserialization

因此,我们正在使用Java,反序列化的对象的结构如下

@ApiModel(description = "Container for collection of Patient Search results")
public class PatientSearchResultListCMRefactor {

private final List<PatientSearchResultCMRefactor> patientSearchResults;
private final int totalSearchResults;
//getters, toString, etc
}

该对象中引用的对象如下

public final class PatientSearchResultCMRefactor {

private float score;

private String id;
private String firstName;
private String lastName;
private String setupDate;

private Optional<AddressCM> address;
private Optional<Boolean> active;
private Optional<String> dateOfBirth;
private Optional<String> email;
private Optional<String> patientExternalId;
private Optional<String> patientReference;
private Optional<String> phoneNumber;
private Optional<String> currentAverageDaysUsed;
private Optional<Double> currentAverageHoursUsed;

private Optional<PatientOrganizationCM> organization;
private Optional<PatientOrganizationCM> topOrganization;

private Optional<ImmutableSet<PatientClinicianCM>> clinicians;

private Optional<ImmutableSet<PatientLocationCMRefactor>> locations;

private Optional<ImmutableSet<PatientDeviceCM>> devices;

private Optional<String> matchesBy;
//setters, getters, left-handed smoke-shifters, constructors 

}

反序列化的代码是

Gson gson = new Gson();
System.out.println(response.getBody());
String jsonTemp = response.getBody();
System.out.println(jsonTemp);
PatientSearchResultListCMRefactor resultListCM = gson.fromJson(jsonTemp, PatientSearchResultListCMRefactor.class);

要反序列化的完整字符串是

{
  "patientSearchResults": [
    {
      "score": 0.7582117,
      "id": "3910dc3d-913b-4862-aee5-4610d2c2981f",
      "firstName": "Nelda",
      "lastName": "Dixon 36",
      "setupDate": "2018-07-19",
      "address": {
        "streetAddress": "0545 Route 202",
        "city": "Joliet",
        "stateProvince": "SC",
        "zipCode": "87636",
        "countryCode": "USA"
      },
      "active": true,
      "dateOfBirth": "1971-01-16",
      "email": "n_dixon_RRAwyqXKIp@example.com",
      "patientExternalId": "6bc63dfa-1106-40c9-875a-a74368cf5189",
      "patientReference": null,
      "phoneNumber": "365-177-2753",
      "currentAverageDaysUsed": null,
      "currentAverageHoursUsed": null,
      "organization": null,
      "topOrganization": null,
      "clinicians": [

      ],
      "locations": null,
      "devices": [

      ],
      "matchesBy": null
    }
],
  "totalSearchResults": 17
}

错误是

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BOOLEAN at line 1 column 289

紧随

"address": {
    "streetAddress": "0545 Route 202",
    "city": "Joliet",
    "stateProvince": "SC",
    "zipCode": "87636",
    "countryCode": "USA"
  },
  "active": true,
字符串中的

,“ true”后的逗号为第289列(为澄清起见,行和列的编号来自于我漂亮打印该字符串之前的行号)。我认为这是一个新对象,但是address是嵌套在顶部对象中的一个子对象,并且子对象的深度永远不会超过1层,尽管每个PatientSearchResultCMRefactor模型都有多个子对象。我是一名测试人员,这是ReST API调用的主体,因此我无法真正更改响应的格式。我所能做的就是尝试按原样解析它。有提示吗?

1 个答案:

答案 0 :(得分:0)

更新:事实证明,gson在使用可选变量和不可变集方面确实遇到了麻烦。一旦我将PatientResultCMRefactor更改为

public final class PatientSearchResultCMRefactor {

private float score;

private String id;
private String firstName;
private String lastName;
private String setupDate;

private AddressCM address;
private Boolean active;
private String dateOfBirth;
private String email;
private String patientExternalId;
private String patientReference;
private String phoneNumber;
private String currentAverageDaysUsed;
private Double currentAverageHoursUsed;

private PatientOrganizationCM organization;
private PatientOrganizationCM topOrganization;

private Set<PatientClinicianCM> clinicians;

private Set<PatientLocationCMRefactor> locations;

private Set<PatientDeviceCM> devices;

private String matchesBy;
//getters, setters, etceterrers
}

一切都完美反序列化。将其发布为答案,而不是更新问题,以便将来其他人更容易找到。