我接受使用request.getParameter("jsondata");
jsondata {"rows":{"1":["2","0","0"],"2":["0","0","0"],"3":["0","0","0"]},"columns":{"1":["2","0","0"],"2":["0","0","0"],"3":["0","0","0"]}}
我会发回这样的数据:
response.setContentType("application/json");
PrintWriter out =response.getWriter();
String jsonStr = "{\"rows\": \""+rowsResult +"\",\"columns\":\""+columnsResult+"\"}";
out.print(jsonStr);
out.flush();
如何计算行和列的总和?
答案 0 :(得分:0)
根据评论更新
输入对象
public class JsonInput {
private ArrayData rows;
private ArrayData columns;
public ArrayData getRows() {
return rows;
}
public void setRows(ArrayData rows) {
this.rows = rows;
}
public ArrayData getColumns() {
return columns;
}
public void setColumns(ArrayData columns) {
this.columns = columns;
}
@Override
public String toString() {
return "JsonInput [rows=" + rows + ", columns=" + columns + "]";
}
}
import java.util.List;
import com.google.gson.annotations.SerializedName;
public class ArrayData {
@SerializedName("1")
private List<Integer> dataOne;
@SerializedName("2")
private List<Integer> dataTwo;
@SerializedName("3")
private List<Integer> dataThree;
public List<Integer> getDataOne() {
return dataOne;
}
public void setDataOne(List<Integer> dataOne) {
this.dataOne = dataOne;
}
public List<Integer> getDataTwo() {
return dataTwo;
}
public void setDataTwo(List<Integer> dataTwo) {
this.dataTwo = dataTwo;
}
public List<Integer> getDataThree() {
return dataThree;
}
public void setDataThree(List<Integer> dataThree) {
this.dataThree = dataThree;
}
@Override
public String toString() {
return "ArrayData [dataOne=" + dataOne + ", dataTwo=" + dataTwo
+ ", dataThree=" + dataThree + "]";
}
}
输出对象
public class JsonOutput {
private SumObj rows;
private SumObj columns;
public SumObj getRows() {
return rows;
}
public void setRows(SumObj rows) {
this.rows = rows;
}
public SumObj getColumns() {
return columns;
}
public void setColumns(SumObj columns) {
this.columns = columns;
}
@Override
public String toString() {
return "JsonOutput [rows=" + rows + ", columns=" + columns + "]";
}
}
import com.google.gson.annotations.SerializedName;
public class SumObj {
@SerializedName("1")
private Integer dataOne;
@SerializedName("2")
private Integer dataTwo;
@SerializedName("3")
private Integer dataThree;
public Integer getDataOne() {
return dataOne;
}
public void setDataOne(Integer dataOne) {
this.dataOne = dataOne;
}
public Integer getDataTwo() {
return dataTwo;
}
public void setDataTwo(Integer dataTwo) {
this.dataTwo = dataTwo;
}
public Integer getDataThree() {
return dataThree;
}
public void setDataThree(Integer dataThree) {
this.dataThree = dataThree;
}
@Override
public String toString() {
return "SumObj [dataOne=" + dataOne + ", dataTwo=" + dataTwo
+ ", dataThree=" + dataThree + "]";
}
}
解析器
import java.lang.reflect.Type;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class Sample {
//{"rows":{"1":["2","0","0"],"2":["0","0","0"],"3":["0","0","0"]},"columns":{"1":["2","0","0"],"2":["0","0","0"],"3":["0","0","0"]}}
private static final String inputJson = "{\"rows\":{\"1\":[\"2\",\"0\",\"0\"],\"2\":[\"0\",\"0\",\"0\"],\"3\":[\"0\",\"0\",\"0\"]},\"columns\":{\"1\":[\"2\",\"0\",\"0\"],\"2\":[\"0\",\"0\",\"0\"],\"3\":[\"0\",\"0\",\"0\"]}}";
public static void main(String[] args) {
Gson gson = new Gson();
Type listType = new TypeToken<JsonInput>() {}.getType();
JsonInput input = gson.fromJson(inputJson, listType);
JsonOutput response = new JsonOutput();
response.setRows(getSumObject(input.getRows()));
response.setColumns(getSumObject(input.getColumns()));
listType = new TypeToken<JsonOutput>() {}.getType();
String output = gson.toJson(response, listType);
System.out.println(output);
}
private static SumObj getSumObject(ArrayData input) {
SumObj obj = new SumObj();
obj.setDataOne(getSum(input.getDataOne()));
obj.setDataTwo(getSum(input.getDataTwo()));
obj.setDataThree(getSum(input.getDataThree()));
return obj;
}
private static Integer getSum(List<Integer> data) {
int sum = 0;
for(Integer i : data){
sum += i;
}
return sum;
}
}