java 对象映射文件_如何将嵌套对象的JSON文件映射到Java对象?

该博客讲述了如何使用Jackson库将包含嵌套对象的JSON字符串映射到Java对象OptionChain。在尝试映射时遇到了UnrecognizedPropertyException,错误提示未找到字段"optionChain"。博主提供了主类、XML配置和相关的Java类代码,并请求帮助解决这个问题。
摘要由CSDN通过智能技术生成

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"])

这里是主类,pop.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 entity = new HttpEntity<>(headers);

ResponseEntity response = restTemplate.exchange(resourceURL, HttpMethod.GET,entity, String.class);

String rawJson = response.getBody();

ObjectMapper objectMapper = new ObjectMapper();

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

}

}

xml·

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.2.RELEASE

com.thompson

OptionsImpliedMovement

0.0.1-SNAPSHOT

OptionsImpliedMovement

Demo project for Spring Boot

1.8

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

org.springframework

spring-web

org.springframework

spring-webmvc

com.fasterxml.jackson.core

jackson-databind

org.springframework.boot

spring-boot-maven-plugin

选项链:

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;

}

以下是

提前感谢您的帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值