在读取json rest api时如何忽略根节点?

时间:2019-02-12 19:24:40

标签: java json spring-boot jackson

我正在阅读Rest API,该API返回 Json 文件。我需要忽略“结果”和“ optionChain”节点。我正在将 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



    }

2 个答案:

答案 0 :(得分:0)

由于您已经获得JSON响应,因此建议使用

restTemplate.exchange(URL, HttpMethod.GET, requestEntity, MyPOJO.class);

代替String.class来定义自己的POJO,它基于您附加在file.json中的JSON响应。

您可以方便快捷地根据http://www.jsonschema2pojo.org/通过JSON生成POJO。

因此它应该看起来像:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"optionChain"
})
public class MyPOJO {

@JsonProperty("optionChain")
private OptionChain optionChain;
// getters and setters 
}

还有一个:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"result",
"error"
})
public class OptionChain {

@JsonProperty("result")
private List<Result> result = null;
@JsonProperty("error")
private Object error;
// getter and setters

}

其他类似:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"expirationDate",
"hasMiniOptions",
"calls",
"puts"
})
public class Option {

@JsonProperty("expirationDate")
private Integer expirationDate;
@JsonProperty("hasMiniOptions")
private Boolean hasMiniOptions;
@JsonProperty("calls")
private List<Call> calls = null;
@JsonProperty("puts")
private List<Put> puts = null;

所以一旦您得到的响应为:

ResponseEntity<MyPOJO> response = restTemplate.exchange(resourceURL, HttpMethod.GET,entity, MyPOJO.class);

然后response.getBody将在optionChain节点中提供您要查找的内容。然后,您通常可以向下钻取到所需的任何节点,因为现在所有内容都位于纯java对象中,并且可以忽略所需的任何内容或使用所需的任何内容。

使用objectMapper还可以实现相同的目标:

 ObjectMapper mapper = new ObjectMapper();
     MyPojo myPojo=  mapper.readValue(rawJson, MyPojo.class);

答案 1 :(得分:0)

快速(高效)并且可以正常工作。

final ObjectMapper objectMapper = new ObjectMapper();
final JsonNode jsonNode = objectMapper.readTree(rawJson);
final JsonNode result = jsonNode.get("optionChain")
                                .get("result");

final JsonNode firstResult = result.get(0);
final YourResultClass resultObject = objectMapper.treeToValue(firstResult, YourResultClass.class);

如果您需要忽略未知字段

final ObjectMapper objectMapper = 
      new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);