2016-07-31 3 views
0

큰 json 파일에서 특정 컬렉션을 가져 오려고하지만 json 파일에서 모든 개체 구조를 만들고 싶지 않습니다. 왜냐하면 " 전화 "와"풋 "필드 ...큰 json 파일에서 특정 컬렉션을 얻는 방법

jsonfile이 있습니다 : https://query2.finance.yahoo.com/v7/finance/options/AEIS?formatted=true&lang=en-US&region=US&corsDomain=finance.yahoo.com

이 내 클래스 옵션 ...입니다 getter 및 setter ..

public class Option { 

    public enum Type { PUT, CALL } 
    @JsonIgnore 
    public Type type; 
    @JsonProperty("contractSymbol") 
    private String contractSymbol; 
    @JsonProperty("contractSize") 
    private String contractSize; 
    @JsonProperty("currency") 
    private String currency; 

    @JsonProperty("inTheMoney") 
    private boolean inTheMoney; 
    @JsonProperty("percentChange") 
    private Field percentChange; 
    @JsonProperty("strike") 
    private Field strike; 
    @JsonProperty("change") 
    private Field change; 
    @JsonProperty("impliedVolatility") 
    private Field impliedVolatility; 
    @JsonProperty("ask") 
    private Field ask; 
    @JsonProperty("bid") 
    private Field bid; 
    @JsonProperty("lastPrice") 
    private Field lastPrice; 

    @JsonProperty("volume") 
    private LongFormatField volume; 
    @JsonProperty("lastTradeDate") 
    private LongFormatField lastTradeDate; 
    @JsonProperty("expiration") 
    private LongFormatField expiration; 
    @JsonProperty("openInterest") 
    private LongFormatField openInterest; 
} 

내가이야을 게시하지 이 같은 데이터를 얻으려고 ...

이 예외가 Aaand
List<Option> res = JSON_MAPPER.readValue(new URL(link), new TypeReference<List<Option>>() {}); 

for(Option o: res){ 
    o.type = Option.Type.CALL; 
    System.out.println(o.toString()); 
} 

...

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token 
    at [Source: https://query2.finance.yahoo.com/v7/finance/options/AEIS?formatted=true&lang=en-US&region=US&corsDomain=finance.yahoo.com; line: 1, column: 16] (through reference chain: java.util.HashMap["optionChain"]) 

답변

0

문제는, 소스에서 json으로 복귀 잭슨 HashMap로 역 직렬화 할 객체 속성 "optionChain"로 시작하지만, deserialized 후 List을 예상합니다.

당신 만 JSON에서 "통화"와 "풋"필요 언급 한 바와 같이, 당신은 다음 JsonNode.findValue에서 "통화"의 노드를 찾아, 먼저 전체 JsonNode를 얻을 수 ObjectMapper.readTree를 사용하고, 마지막 노드를 직렬화 할 수 있습니다. 다음은 예입니다.

String link = "https://query2.finance.yahoo" + 
      ".com/v7/finance/options/AEIS?formatted=true&lang=en-US&region=US&corsDomain=finance.yahoo.com"; 
ObjectMapper mapper = new ObjectMapper(); 
JsonNode jsonNode = mapper.readTree(new URL(link)); 
JsonNode calls = jsonNode.findValue("calls"); 
List<Option> callOptions = mapper.readValue(calls.traverse(), new TypeReference<List<Option>>() {}); 
관련 문제