2016-07-31 25 views
0

Ich versuche, eine bestimmte Sammlung von einer großen JSON-Datei zu bekommen, aber ich möchte nicht die gesamte Objektstruktur auf der JSON-Datei machen, weil ich nur die " Anrufe“und "puts" Felder ...So erhalten Sie eine bestimmte Sammlung von einer großen JSON-Datei

die jsonfile ist dies: https://query2.finance.yahoo.com/v7/finance/options/AEIS?formatted=true&lang=en-US&region=US&corsDomain=finance.yahoo.com

diese meine Klassenoptionen ist ... nicht Getter und Setter veröffentlichen ..

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; 
} 

und ich bin versuchen, Daten wie diese zu bekommen ...

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()); 
} 

aaand ist dies die Ausnahme ...

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

Antwort

0

Das Problem ist, dass die json Rückkehr von der Quelle mit einer Objekt-Eigenschaft beginnt, „optionChain“, die Jackson versuchen, es so HashMap deserialisieren, aber Sie erwarten eine List nach deserialized.

Wie Sie erwähnt nur die „Anrufe“ und „Puts“ in der json benötigen, können Sie ObjectMapper.readTree verwenden, um die ganze JsonNode zuerst zu erhalten, dann den Knoten von „Anrufe“ finden von JsonNode.findValue und schließlich den Knoten deserialisieren. Es folgt ein Beispiel:

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>>() {});