Java流来操作JSON

时间:2019-05-12 20:05:37

标签: java json list java-stream

我有一个包含以下内容的JSON数组:

[
{
  "responseId": 10,
  "userId": 2,
  "query": {
    "queryId": 1,
    "query": "acne"
  },
  "surveyId": 1,
  "response": "good"
},
{
  "responseId": 11,
  "userId": 2,
  "query": {
    "queryId": 2,
    "query": "asthma"
  },
  "surveyId": 1,
  "response": "number1"
},
{
  "responseId": 12,
  "userId": 2,
  "query": {
    "queryId": 3,
    "query": "bell"
  },
  "surveyId": 1,
  "response": "bad"
},
{
  "responseId": 13,
  "userId": 1,
  "query": {
    "queryId": 1,
    "query": "acne"
  },
  "surveyId": 1,
  "response": "good"
},
{
  "responseId": 14,
  "userId": 1,
  "query": {
    "queryId": 2,
    "query": "asthma"
  },
  "surveyId": 1,
  "response": "number1"
},
{
  "responseId": 15,
  "userId": 1,
  "query": {
    "queryId": 3,
    "query": "bell"
  },
  "surveyId": 1,
  "response": "bad"
},
{
  "responseId": 16,
  "userId": 3,
  "query": {
    "queryId": 1,
    "query": "acne"
  },
  "surveyId": 1,
  "response": "bad"
},
{
  "responseId": 17,
  "userId": 3,
  "query": {
    "queryId": 2,
    "query": "asthma"
  },
  "surveyId": 1,
  "response": "good"
},
{
  "responseId": 18,
  "userId": 3,
  "query": {
    "queryId": 3,
    "query": "bell"
  },
  "surveyId": 1,
  "response": "number2"
}
]

我想要以某种类似于JSON的方式来处理该JSON:

[
{
  "queryId": 1,
  "query": "acne",
  "totalResponse": 3,
  "good": 2,
  "bad": 1,
  "number1": 0,
  "number2": 0
},
{
  "queryId": 2,
  "query": "asthma",
  "totalResponse": 3,
  "good": 1,
  "bad": 0,
  "number1": 2,
  "number2": 0
},
{
  "queryId": 3,
  "query": "bell",
  "totalResponse": 3,
  "good": 0,
  "bad": 2,
  "number1": 0,
  "number2": 1
}
]

我创建了一个POJO类,可以存储所需的JSON。该类看起来像:

public class AggregateResponse {

private int queryId;
private String query;
private int totalResponse;
private int good;
private int bad;
private int number1;
private int number2;

public int getQueryId() {
    return queryId;
}

public void setQueryId(int queryId) {
    this.queryId = queryId;
}

public String getQuery() {
    return query;
}

public void setQuery(String query) {
    this.query = query;
}

public int getGood() {
    return good;
}

public void setGood(int good) {
    this.good = good;
}

public int getBad() {
    return bad;
}

public void setBad(int bad) {
    this.bad = bad;
}

public int getNumber1() {
    return number1;
}

public void setNumber1(int number1) {
    this.number1 = number1;
}

public int getNumber2() {
    return number2;
}

public void setNumber2(int number2) {
    this.number2 = number2;
}

public int getTotalResponse() {
    return totalResponse;
}

public void setTotalResponse(int totalResponse) {
    this.totalResponse = totalResponse;
}

}

经过一些研究,我觉得可以使用流有效地完成此任务。

List<AggregateResponse> list = responses.stream((resp) => {
        // TODO
});

在这里,“ respons”是第一个JSON,我想以使其返回所需JSON的方式进行操作。我不确定,使用流可以如何实现这一目标。谁能指导我?

1 个答案:

答案 0 :(得分:0)

您可以使用map函数来转换数据。然后,您可以使用collect函数将流内容收集到一个列表中。

List<AggregateResponse> list = responses.stream()
    .map(response -> {/* your transformation goes here*/})
    .collect(Collectors.toList());

转换步骤采用单个响应对象,并返回一个AggregateResponse。实际的实现取决于您的json解析代码。