如何将带有嵌套对象的复杂json文件映射到Java对象?

时间:2019-02-12 16:50:12

标签: java json spring-boot jackson jackson-databind

我有一个复杂的 Json 文件,该文件由几个嵌套对象和对象数组组成。第一个对象是“ OptionChain”,具有一个名为“ Result”的对象。 “结果”具有嵌套的对象:“报价”和“选项”。最后,“选项”具有嵌套的对象数组,分别命名为“调用”和“放置”。

我用@JSonProperty注释了所有类变量,并使用 Spring Boot Jackson 处理对象映射。我是使用 Jackson 和对象映射的新手。

运行程序时,出现此错误:

Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "optionChain" (class com.thompson.OptionsImpliedMovement.data.OptionChain), not marked as ignorable (one known property: "result"])
 at [Source: (String)"{"optionChain":{"result":[{"underlyingSymbol":"KO","expirationDates":[1550188800,1550793600,1551398400,1552003200,1552608000,1553212800,1553817600,1555545600,1558051200,1561075200,1565913600,1579219200,1610668800],"strikes":[37.0,38.0,40.5,41.5,42.5,43.5,44.5,45.5,46.5,47.5,48.5,49.5,50.5,51.0,51.5,52.0,53.0,53.5,54.0],"hasMiniOptions":false,"quote":{"language":"en-US","region":"US","quoteType":"EQUITY","quoteSourceName":"Nasdaq Real Time Price","currency":"USD","exchangeDataDelayedBy":0,"earnin"[truncated 10817 chars]; line: 1, column: 17] (through reference chain: com.thompson.OptionsImpliedMovement.data.OptionChain["optionChain"])

这是主要类pom.xml和我的两个Java类:

主要:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.thompson.OptionsImpliedMovement.data.OptionChain;
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 = "https://query2.finance.yahoo.com/v7/finance/options/ko";
        HttpEntity<String> entity = new HttpEntity<>(headers);
        ResponseEntity<String> response = restTemplate.exchange(resourceURL, HttpMethod.GET,entity, String.class);

        String rawJson = response.getBody();

        ObjectMapper objectMapper = new ObjectMapper();

        OptionChain optionChain = objectMapper.readValue(rawJson, OptionChain.class);

    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.thompson</groupId>
    <artifactId>OptionsImpliedMovement</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>OptionsImpliedMovement</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

OptionChain:

import com.fasterxml.jackson.annotation.JsonProperty;

public class OptionChain {
    @JsonProperty("result")
    public Result result;

}    

结果:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty; 

public class Result  {

    @JsonProperty("underlyingSymbol")
    public String symbol;
    @JsonProperty("expirationDates")
    public long[] expirationDates;
    @JsonProperty("strikes")
    public double[] strikes;
    @JsonProperty("hasMiniOptions")
    public boolean hasMiniOptions;
    @JsonProperty("quote")
    public Quote quote;
    @JsonProperty("options")
    public Options option;
}

这是 Json 文件的层次结构的屏幕截图: Hierarchy of Json

最后是完整的 Json 文件: Full Json File

在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

在我看来,您正在将{ "optionChain": {...} }的JSON解组,但是您直接在OptionChain中这样做。相反,您需要定义一个具有单个OptionChain成员的类,因为您正在拆封包含此optionChain字段的外部对象(包围整个响应的{ }是该对象您正试图解组)。

例如,

public class OptionChainResponse {

   @JsonProperty("optionChain")
   private OptionChain optionChain;

   // getter/setter
}

然后:

OptionChainResponse optionChainResponse = objectMapper.readValue(rawJson, OptionChainResponse.class);
// do some validation or checking maybe
OptionChain optionChain = optionChainResponse.getOptionChain();