为什么在尝试打印Cover数组时为什么输出为null?

时间:2018-08-22 14:42:44

标签: java json list gson

我正在使用GSON解析JSON文件,但是当我打印Cover数组内的元素时,我得到了null作为输出。 我需要coverNamecoverIDbenefitsIncluded

我确定我在某个地方做傻事。请查看相关文件。

request.json

{
"email": "someone@example.com",
"firstName": "someone",
"lastname": "",
"policyNumber": "PHD121213213123Q1",
"premiumAmount": "200.00",
"premiumFrequency": "Monthly",
"cover": [{
        "coverName": "Child Critical Illness",
        "coverID": 10000,
        "benefitsIncluded": [
            "Lump sum payment",
            "No waiting period",
            "Unlimited claim limit per policy owner"
        ]
    },
    {
        "coverName": "Life Coverage",
        "coverID": 10001,
        "benefitsIncluded": [
            "Lump sum benefit should the life insured pass away",
            "Max of 1 Mn per Home Loan",
            "Max 2 Mn per policy Owner",
            "Up to $50k per account for each Credit Card and Personal Loan"
        ]
    },
    {
        "coverName": "Critical Illness",
        "coverID": 10002,
        "benefitsIncluded": [
            "lump sum payment of 12 monthly disability payment",
            "max of 120K per home loan account",
            "max of 50K per credit card and personal loan "
        ]
    }
]
}

Insured.java

public class Insured {
private String email;
private String firstName;
private String lastname;
private String policyNumber;
private String applicationPageLink;
private String premiumAmount;
private String premiumFrequency;
private List<Cover> cover;
//Getters and Setters

Cover.java

public class Cover {
    private String coverName;
    private String coverId;
    private List<String> Benefits;
    //Getters and Setters
}

主要方法

public static void main( String[] args ) throws FileNotFoundException {

    InputStream in = new FileInputStream("json file path");
    BufferedReader buffer=new BufferedReader(new InputStreamReader(in));

    Insured insured = new Gson().fromJson(buffer, Insured.class);

    for (Cover cover : insured.getCover()) {
        System.out.println(cover.getBenefits());
        System.out.println(cover.getCoverId());
    }
}

1 个答案:

答案 0 :(得分:1)

获取null的原因是GSON无法将Cover对象中的字段与JSON中的字段进行匹配,因为属性名称不同(尤其是您拥有coverIdcoverIDBenefitsbenefitsIncluded)。

要解决此问题,您有2种选择:

  1. 重命名您的Java字段,使其与JSON中的字段完全匹配,如下所示:

    class Cover {
        private String coverName;
        private String coverID;
        private List<String> benefitsIncluded;
    
        //Getters and Setters
        ...
    }
    

    但是,此解决方案不是很好,因为它可能使您不遵循Java命名约定。

  2. 为每个具有不同名称的字段添加@SerializedName("xxx")批注,其中xxx是JSON中属性的名称,如下所示:

    class Cover {
        private String coverName;
        @SerializedName("coverID")
        private String coverId;
        @SerializedName("benefitsIncluded")
        private List<String> benefits;
    
        //Getters and Setters
        ....
    }
    

    请注意,使用此解决方案,您不必强制为每个字段添加@SerializedName注释,而只能为Java和JSON之间具有不同名称的字段添加

在两种情况下,您最终都会得到以下输出:

[Lump sum payment, No waiting period, Unlimited claim limit per policy owner]
10000
[Lump sum benefit should the life insured pass away, Max of 1 Mn per Home Loan, Max 2 Mn per policy Owner, Up to $50k per account for each Credit Card and Personal Loan]
10001
[lump sum payment of 12 monthly disability payment, max of 120K per home loan account, max of 50K per credit card and personal loan ]
10002