将Json字符串反序列化为对象java

Posted

技术标签:

【中文标题】将Json字符串反序列化为对象java【英文标题】:Deserialize Json string to object 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 拼写错误,请检查。 这在我的模型类中是正确的。这里可能是一个错字。 基本账户是一个对象而不是一个列表 【参考方案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;


【讨论】:

以上是关于将Json字符串反序列化为对象java的主要内容,如果未能解决你的问题,请参考以下文章

将json字符串反序列化为python中的对象

无法将 JSON 字符串反序列化为 C# 对象

System.Text.Json - 将嵌套对象反序列化为字符串

将Json字符串反序列化为对象java

将JSON字符串反序列化为指定的.NET对象类型

将嵌套的 JSON 字符串反序列化为 Java 对象