2015-12-23 2 views
0

У меня есть апи, что выплевывает следующий JSON:Как разобрать массив массива JSON в Джексон JSON парсер

{ 
    "monitors": [ 
     [ 
      "/Common/http-cc-ping-any" 
     ] 
    ], 
    "is_alive":true 
} 

Я пытаюсь использовать весной и Джексон JSON Parser для отображения JSON в POJO

POJO определяется как:

public class MonitorsList { 

    @JsonProperty("monitors") 
    private List<Monitors> monitors; 

    public void setMonitors(List<Monitors> monitors) { 
     this.monitors = monitors; 
    } 

    public List<Monitors> getMonitors() { 
     return this.monitors; 
    } 

    private boolean is_alive; 

    public void setIsAlive(boolean is_alive) { 
     this.is_alive= is_alive; 
    } 

    public boolean getIsAlive() { 
     return this.is_alive; 
    } 
} 

и мой монитор POJO является:

@JsonAutoDetect 
public class Monitors { 
    private ArrayList<String> monitors; 

    public ArrayList<String> getMonitors() { 
      return this.monitors; 
     } 

     public void setMonitors(ArrayList<String> monitors) { 
      this.monitors = monitors; 
     } 
} 

Я использую RestTemplate для извлечения данных в формате JSON, но я получаю следующее сообщение об ошибке:

Error: Could not read document: Can not deserialize instance of hello.Monitors out of START_ARRAY token at [Source: [email protected]; line: 1, column: 14] (through reference chain: hello.MonitorsList ["monitors"]->java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of hello.Monitors out of START_ARRAY token at [Source: [email protected]; line: 1, column: 14] (through reference chain: hello.MonitorsList ["monitors"]->java.util.ArrayList[0])

Я не могу показаться, чтобы выяснить, что я делаю неправильно.

Любая помощь будет оценена!

+0

У вас есть массив JSON массивов JSON строк JSON. Как вы ожидаете, что для сопоставления с «списком »? –

+0

Я попытался использовать ArrayList > содержание в Greeting.java, но все равно не работает –

+0

Вы имеете в виду в 'MonitorsList'? –

ответ

0

Благодаря @SotiriosDelimanolis и небольшому чтению я смог выяснить, что я делаю неправильно.

Итак, JacksonJSON Parser отображает

{ 
    "monitors": [ 
     [ 
      "/Common/http-cc-ping-any" 
     ] 
    ], 
    "is_alive":true 
} 

как следующий POJO:

public class MonitorsList { 

    @JsonProperty("monitors") 
    private List<Monitors> monitors; 

    public void setMonitors(List<Monitors> monitors) { 
     this.monitors = monitors; 
    } 

    public List<Monitors> getMonitors() { 
     return this.monitors; 
    } 

    @JsonProperty("is_alive") 
    private boolean is_alive; 

    public void setIsAlive(boolean is_alive) { 
     this.is_alive= is_alive; 
    } 

    public boolean getIsAlive() { 
     return this.is_alive; 
    } 
} 

И монитор POJO как:

@JsonAutoDetect 
public class Monitors extends ArrayList<String> { 
} 

Примечание что is_alive не делает карта на setIsAlive, поэтому предоставив аннотацию @JsonProperty("is_alive") либо в поле is_alive, либо по методу setIsAlive будет работать!

+2

Обратите внимание, что я бы не рекомендовал создавать подкласс 'ArrayList'. Композиция, как правило, лучше. Посмотрите на '@ JsonCreator'. –

+0

Спасибо за предложение. –

Смежные вопросы