Десериализовать строку Json в объект Java - PullRequest
0 голосов
/ 05 октября 2019

Я получаю ответ в виде строки правильно. Но когда я пытаюсь смоделировать это для своего модельного класса, я не могу получить значения. Я не сериализовал его должным образом, я думаю.

Я пробовал некоторые другие примеры, но он говорит:

Невозможно десериализовать экземпляр из START_OBJECT

Мой ответ на JsonString:

{
    "basicAccount": {
        "acctId": 101600000,
        "edjAccountPreferredName": "MARET, RODEL",
        "faNumber": "000000",
        "branchNumber": 0000,
        "relationshipId": 00000000,
        "customerNo": 88888888,
        "primaryContactId": 14532683,
        "accountCloseDate": null,
        "authFormReceivedInd": "Y",
        "accountServiceTypeCode": null,
        "accountServiceTypeStatusCode": null,
        "accountOwnershipRuleId": 321,
        "inheritedAccountFlag": "N",
        "ownershipTypeKey": "US_FD_T",
        "accountMailingAddressId": 53828912,
        "vendorAccountId": "262826",
        "ownershipTypeDescription": "hghfdbgh hjksd   ",
        "countryCode": null,
        "accountOpenDate": "1995-08-11"
    },
    "trustAccount": false
}

/////////////////////// Моя модель Класс: //////////////////////

public class BasicAccount implements Serializable {

    @JsonProperty("basicAccount")
    @JsonInclude(Include.NON_NULL)
    private List<BasicAccount> basicAccount;

    List<AccountResource> basicAccounts;*/

    @JsonProperty("acctId")
    private BigDecimal acctId;

    @JsonProperty("edjAccountPreferredName")
    private String edjAccountPreferredName;

    @JsonProperty("faNumber")
    private String faNumber;

    @JsonProperty("branchNumber")
    private Integer branchNumber;

    @JsonProperty("relationshipId")
    private BigDecimal relationshipId;

    @JsonProperty("customerNo")
    private BigDecimal customerNo;

    @JsonProperty("primaryContactId")
    private BigDecimal primaryContactId;

    @JsonProperty("accountCloseDate")
    private String accountCloseDate;

    @JsonProperty("authFormReceivedInd")
    private String authFormReceivedInd;

    @JsonProperty("accountServiceTypeCode")
    private String accountServiceTypeCode;

    @JsonProperty("accountServiceTypeStatusCode")
    private String accountServiceTypeStatusCode;

    @JsonProperty("accountOwnershipRuleId")
    private Short accountOwnershipRuleId;

    @JsonProperty("inheritedAccountFlag")
    private String inheritedAccountFlag;

    @JsonProperty("ownershipTypeKey")
    private String ownershipTypeKey;

    @JsonProperty("accountMailingAddressId")
    private BigDecimal accountMailingAddressId;

    @JsonProperty("vendorAccountId")
    private String vendorAccountId;

    @JsonProperty("ownershipTypeDescription")
    private String ownershipTypeDescription;

    @JsonProperty("countryCode")
    private String countryCode;

    @JsonProperty("accountOpenDate")
    private String accountOpenDate;

    @JsonInclude(Include.NON_NULL)
    private boolean trustAccount;


    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);

    }
}

Код, используемый для десериализации, приведен ниже:

String basiStr =  restTemplate.exchange(targetUrl,HttpMethod.GET,requestEntity, new ParameterizedTypeReference<String>(){}).getBody();
BasicAccount myProduct = objectMapper.readValue(basiStr,new TypeReference<BasicAccount>(){});

1 Ответ

0 голосов
/ 05 октября 2019

Я думаю, что ваша модель данных немного смещена. Не могли бы вы попробовать с моделью ниже:

public class SomeDifferentName implements Serializable {
    @JsonProperty("basicAccount") BasicAccount basicAccount;
    @JsonInclude(Include.NON_NULL) private boolean trustAccount;
}

и в другом классе:

public class BasicAccount implements Serializable {

    @JsonProperty("acctId")
    private BigDecimal acctId;

    @JsonProperty("edjAccountPreferredName")
    private String edjAccountPreferredName;

    @JsonProperty("faNumber")
    private String faNumber;

    @JsonProperty("branchNumber")
    private Integer branchNumber;

    @JsonProperty("relationshipId")
    private BigDecimal relationshipId;

    @JsonProperty("customerNo")
    private BigDecimal customerNo;

    @JsonProperty("primaryContactId")
    private BigDecimal primaryContactId;

    @JsonProperty("accountCloseDate")
    private String accountCloseDate;

    @JsonProperty("authFormReceivedInd")
    private String authFormReceivedInd;

    @JsonProperty("accountServiceTypeCode")
    private String accountServiceTypeCode;

    @JsonProperty("accountServiceTypeStatusCode")
    private String accountServiceTypeStatusCode;

    @JsonProperty("accountOwnershipRuleId")
    private Short accountOwnershipRuleId;

    @JsonProperty("inheritedAccountFlag")
    private String inheritedAccountFlag;

    @JsonProperty("ownershipTypeKey")
    private String ownershipTypeKey;

    @JsonProperty("accountMailingAddressId")
    private BigDecimal accountMailingAddressId;

    @JsonProperty("vendorAccountId")
    private String vendorAccountId;

    @JsonProperty("ownershipTypeDescription")
    private String ownershipTypeDescription;

    @JsonProperty("countryCode")
    private String countryCode;

    @JsonProperty("accountOpenDate")
    private String accountOpenDate;

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...