【问题标题】:Deserialize Json string to object java将Json字符串反序列化为对象java
【发布时间】:2020-02-03 12:15:20
【问题描述】:

我得到了正确的字符串形式的响应。但是当我尝试将它建模到我的模型类时,我无法获得这些值。我猜我没有正确序列化它。

我尝试了其他一些示例,但它说:

无法反序列化 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
}

//////////////////// 我的模型类: ///////////////////////p>

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>(){});

【问题讨论】:

  • 我认为您的班级中 edpAccountPreferredName 拼写错误,请检查。
  • 这在我的模型类中是正确的。这里可能是一个错字。
  • 基本账户是一个对象而不是一个列表

标签: json string spring-boot object serialization


【解决方案1】:

我认为您的数据模型有点偏移。你能试试下面的模型吗:

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;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多