【发布时间】:2019-02-12 19:24:39
【问题描述】:
我正在阅读一个返回 Json 文件的 rest api。我需要忽略“结果”和“选项链”节点。我正在使用 Spring Boot 和 Jackson 来处理对象的映射。
提前致谢!
对于 Json 文件click here
这是我的主要内容:
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;
@SpringBootApplication
public class OptionsImpliedMovementApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(OptionsImpliedMovementApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
String resourceURL = url;
HttpEntity<String> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(resourceURL, HttpMethod.GET,entity, String.class);
String rawJson = response.getBody();
ObjectMapper mapper = new ObjectMapper();
//need to read in json ignoring root node
}
【问题讨论】:
-
所以基本上你想忽略整个 JSON?
-
没有问题,当我尝试将 JSON 对象映射到 java 对象时,它认为结果是一个数组。我只需要读入结果节点之后的信息。
-
之后还是里面? “结果”实际上是一个数组。为什么不简单地更改您的 JSON 文档?
-
反正我现在在健身房,等会儿我给你sn-p。
-
是的,我想在“结果”中阅读。但是我不想更改 JSON 文档,因为每次我请求 rest api 时,我都会继续收到这种样式的 JSON 文档。所以我试图找到一种方法来只读取结果中的内容。由于“结果”是一个只有一个插入的数组,我觉得创建一个结果数组而只读取里面的内容是没有意义的。
标签: java json spring-boot jackson