如何从 HttpServletRequest 读取 JSON 数组?

How to read JSON array from HttpServletRequest?

我是 servlet 的新手,我无法从 HttpServletRequest

读取 JSON 数组

我正在 JSON 下方发送至 Java

page: 1
start: 0
limit: 20
sort: [{"property":"fiscalYear","direction":"DESC"}]
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String[] s=request.getParameterValues("sort");
        for(int i=0;i<s.length;i++)
}           System.out.println(s[i]);

实际产量

 [{"property":"fiscalYear","direction":"DESC"}]

预期输出分别获取值 fiscalYear 和 DESC

您可以创建一个包含此信息的对象:

public class SortDto {

  private String property;
  private String direction;

  // getters, setters, toString() ..
}

然后像这样创建一个 ObjectMapper :

ObjectMapper mapper = new ObjectMapper();
String sortJson = request.getParameter("sort");
// I suppose that sortJson is => {"property":"fiscalYear","direction":"DESC"}
SortDto dto = mapper.readValue(sortJson, SortDto.class);

然后您可以覆盖 class 中的 toString() 方法或调用 dto.getProperty() dto.getDirection() 单独获取值。


备注

我使用 request.getParameter("sort") 其中 return 一个字符串而不是 request.getParameterValues("sort") 其中 return 一个值数组

getParameterValues(String name) 将 return 字符串数组

String[] getParameterValues(String name)

Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.

If the parameter has a single value, the array has a length of 1.

getParameter(String name) 只会 return String

Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value, use getParameterValues(java.lang.String).

基于此你可以选择 getParameter 其中 returns JSON 表示字符串

String s=request.getParameter("sort"); // {"property":"fiscalYear","direction":"DESC"}

现在使用ObjectMapper读取解析JSON字符串

ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(s);
String property = jsonNode.get("property").asText();
String direction = jsonNode.get("direction").asText();

如果是JsonObjectsArray //[{"property":"fiscalYear","direction":"DESC"}]

JsonNode jsonNode = objectMapper.readTree(s);
JsonNode node = jsonNode.get(0);
String property = node.get("property").asText();
String direction = node.get("direction").asText();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String str=request.getParameterValues("sort");
    //  str=" [{\"property\":\"fiscalYear\",\"direction\":\"DESC\"}]";   
      JSONArray array=new JSONArray(str);
      for(int i=0;i<array.length();i++){
              JSONObject json_obj = array.getJSONObject(i);
            System.out.println( json_obj.getString("direction"));

      }
} 

输出

描述