简体   繁体   English

如何获取和解析来自 x-www-form-urlencoded POST、RestTemplate (Java) 的 JSON 响应?

[英]How to get and parse JSON response from x-www-form-urlencoded POST, RestTemplate (Java)?

I have this method to make request:我有这个方法来提出请求:

    @Override
    public HttpEntity<MultiValueMap<String, String>> generateRequestEntity(Date date) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("key", "SOME_API_KEY");
        map.add("source", "SOME_API_SOURCE");
        if (date == null)
            map.add("method", "getall");
        else {
            map.add("method", "getfrom");
            map.add("date", new SimpleDateFormat("yyyy-MM-dd").format(date));
        }

        return new HttpEntity<>(map, headers);
    }

I send a request and get a response, as recommended at following link: URL我发送一个请求并得到一个响应,如以下链接所推荐的: URL

HttpEntity<MultiValueMap<String, String>> request = generateRequestEntity(date);
ResponseEntity<OnlineSell[]> response = restTemplate.postForEntity(url, request, OnlineSell[].class);
OnlineSell[] onlineSells = response.getBody();

But I have a problem.但我有一个问题。 I am failing when trying to parse JSON-response.尝试解析 JSON 响应时失败。 OnlineSell - class, that must keep the answer BUT I just can't create a structure that successfully stores the values of this answer. OnlineSell - class,必须保留答案但我无法创建成功存储此答案值的结构。 A very large and complex answer tree comes.一个非常大而复杂的答案树来了。

Answer: Can I get JSONObject from it to parse and save manually ?答:我可以从中获取 JSONObject 以手动解析和保存吗? Or can I get help with JSON parsing by previously updating this post and adding it ( answer form Postman )?或者我可以通过之前更新这篇文章并添加它(回答表格 Postman )来获得 JSON 解析的帮助吗?

What you can do is to consider the ResponseEntity as a String .您可以做的是将 ResponseEntity 视为String Then afterwards you can use objectMapper with readValue() ,然后,您可以将objectMapperreadValue()一起使用,

Something like this:像这样的东西:

ResponseEntity<String> response = restTemplate().postForEntity(url, request, String.class);

String body = response.getBody();
OnlineSell[] onlineSells = new ObjectMapper().readValue(body, OnlineSell[].class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以 application/x-www-form-urlencoded 在 restTemplate 中发送正文 - How to send body in restTemplate as application/x-www-form-urlencoded 如何使用Android批注和RestTemplate在POST请求的正文中发送x-www-form-urlencoded - How to send x-www-form-urlencoded in a body of POST request using android annotations and resttemplate POST 调用接受 x-www-form-urlencoded 但拒绝 JSON - POST call accepts x-www-form-urlencoded but refuses JSON 使用 Java Unirest 发布“x-www-form-urlencoded”实体 - Post an 'x-www-form-urlencoded' entity with Java Unirest 如何使用关联数组解析 application/x-www-form-urlencoded? - How to parse a application/x-www-form-urlencoded with an associative array? POST x-www-form-urlencoded - POST x-www-form-urlencoded 如何使用 webclient 发布正文 x-www-form-urlencoded? - how to post body x-www-form-urlencoded using webclient? 如何使用 x-www-form-urlencoded 正文发送 post 请求 - How to send post request with x-www-form-urlencoded body 如何在Java中将字符串转换为x-www-form-urlencoded? - How to convert a string to x-www-form-urlencoded in Java? Spring RestTemplate如何在application / x-www-form-urlencoded中使用POJO? - Spring RestTemplate how to use POJO with application/x-www-form-urlencoded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM