从已经用json文件中的数据填充的spring bean中计算平均评分

时间:2019-04-08 14:43:31

标签: java

我有一个json数据,该数据已加载到Spring bean AuthorRatingResponse

 [
      {
        "averageRating": 4.0,
        "author": [
          "Clifford Geertz"
        ]
      },
      {
        "averageRating": 4.5,
        "author": [
          "Sir Thomas Stamford Raffles"
        ]
      },
      {
        "averageRating": 0.0,
        "author": [
          "Cay S. Horstmann",
          "Gary Cornell"
        ]
      },
      {
        "averageRating": 0.0,
        "author": [
          "Cay S. Horstmann",
          "Gary Cornell"
        ]
      },
      {
        "averageRating": 1.0,
        "author": [
          "James William Bayley Money"
        ]
      },
      {
        "averageRating": 4.0,
        "author": [
          "Bruce Eckel"
        ]
      },
      {
        "averageRating": 0.0,
        "author": [
          "Patrick Henry Winston",
          "Sundar Narasimhan"
        ]
      } 
    ]

我想计算每个作者的平均评分,这样我就不会有一组作者,而只有一个作者和一个评分,下面的示例 我的输出最后应该像这样

[
  {
    "averageRating": 4.0,
    "author": "Clifford Geertz"
  },
  {
    "averageRating": 4.5,
    "author": "Sir Thomas Stamford Raffles"
     },
  {
    "averageRating": 0.0,
    "author": "Cay S. Horstmann"
  }

如上所述,我没有数组,只有单个名称和等级,但是我不确定如何编写算法。请注意,作为一个数组的bean AuthorRatingResponse已经包含了上面所有以作者为数组的json日期, 我如何计算每个作者的平均评分,此方法应返回仅作者和评分的列表 这是POJO

@Getter
@Setter
public class AuthorRatingResponse {

    private List<String> Author;
    private double averageRating;

    public AuthorRatingResponse(List<String> author, double averageRating) {
        Author = author;
        this.averageRating = averageRating;
    }

    public AuthorRatingResponse() {
    }

}

0 个答案:

没有答案