gpt4 book ai didi

java - Servlet 使用 getParameter 获取 json 数组

转载 作者:太空宇宙 更新时间:2023-11-04 14:53:52 26 4
gpt4 key购买 nike

我使用 request.getParameter("jsondata"); 向 servlet 接收这样的数组

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();

如何计算行和列的总和?

最佳答案

根据评论更新

输入对象

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;
}
}

关于java - Servlet 使用 getParameter 获取 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23432214/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com