【问题标题】:How to read JSON array from HttpServletRequest?如何从 HttpServletRequest 读取 JSON 数组?
【发布时间】:2019-10-24 01:51:41
【问题描述】:

我对 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"}]

预期的输出分别获得财政年和DESC值

【问题讨论】:

  • 使用它来生成领域模型类:jsonutils.com 然后使用 ObjectMapper 将 json 转换为对象

标签: java json servlets jackson objectmapper


【解决方案1】:

您可以创建一个保存此信息的对象:

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

然后你可以在你的类中重写toString() 方法或调用dto.getProperty() dto.getDirection() 来分别获取值。


注意

我使用了request.getParameter("sort"),它返回一个字符串,而不是request.getParameterValues("sort"),它返回一个值数组

【讨论】:

  • 它是一个数组 [ {"property":"fiscalYear","direction":"DESC"}]
  • @AnmolDuggal 即使使用[ {"property":"fiscalYear","direction":"DESC"}] 我的解决方案也可以正常工作,我对它们都进行了测试并且都可以正常工作
【解决方案2】:

getParameterValues(String name) 将返回字符串数组

String[] getParameterValues(String name)

返回一个字符串对象数组,其中包含给定请求参数的所有值,如果参数不存在,则返回 null。

如果参数只有一个值,则数组长度为1。

getParameter(String name) 将只返回 String

将请求参数的值作为字符串返回,如果参数不存在,则返回 null。请求参数是随请求发送的额外信息。对于 HTTP servlet,参数包含在查询字符串或发布的表单数据中。

只有在确定参数只有一个值时才应使用此方法。如果参数可能有多个值,请使用 getParameterValues(java.lang.String)。

基于此,您可以选择getParameter,它返回代表字符串的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();

如果是ArrayJsonObjects //[{"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();

【讨论】:

    【解决方案3】:
    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"));
    
          }
    } 
    

    输出

    降序

    【讨论】:

      猜你喜欢
      • 2018-11-17
      • 2017-02-07
      • 2018-01-24
      • 2021-12-13
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 2019-11-11
      • 2018-05-16
      相关资源
      最近更新 更多