当我在响应中添加硬编码0时,在响应中得到“ NaN”

时间:2019-07-02 09:45:14

标签: json spring-boot spring-data-jpa

我正在尝试为JSON响应中的对象获取硬编码的响应0,但是我在响应中得到“ NaN”。

if (ratings != null || ratings.size() > 0) {
                    averageRating = 0.0;
                    for (Rating rating : ratings) {
                        averageRating += rating.getRating();
                    }
                    averageRating /= ratings.size();

                    JSONObject response2 = new JSONObject();

                    response2.put("sellerId", seller.getId());
                    response2.put("sellerName", seller.getName());
                    response2.put("picture", seller.getPicture());
                    response2.put("avg", String.format("%.1f", averageRating));

                    response.put(response2);

                } else {
                    JSONObject response2 = new JSONObject();

                    response2.put("sellerId", seller.getId());
                    response2.put("sellerName", seller.getName());
                    response2.put("picture", seller.getPicture());
                    response2.put("avg", "0.0");

                    response.put(response2);
                }

这是我得到的答复

[
    {
        "sellerId": 1,
        "avg": "4.0",
        "sellerName": "mr.a",
        "picture": "resources/images/user/20190526_1/1.jpg"
    },
    {
        "sellerId": 2,
        "avg": "3.9",
        "sellerName": "mr.b",
        "picture": "resources/images/user/20190526_1/2.jpg"
    },
    {
        "sellerId": 3,
        "avg": "3.5",
        "sellerName": "mr.c",
        "picture": "resources/images/user/20190526_1/3.png"
    },
    {
        "sellerId": 5,
        "avg": "NaN",
        "sellerName": "mr.d",
        "picture": "resources/images/user/20190526_1/5.png"
    }
]

平均价值对于sellerId 1,2,3来说是完美的。 如果卖方没有任何评价,则应该显示为0。但卖方ID为5则显示为Nan。

在此方面将提供任何帮助。

0 个答案:

没有答案