I have to get data from a web service, I'm using Jackson but I have the same problem using Gson, I have no problem with single objects but when I receive several objects list it is not that easy for me.
JSON received are like this:
{"country":
[
{"code":"AD","nombre":"Andorra","name":"Andorra"},
{"code":"AE","nombre":"Emiratos Árabes Unidos","name":"United Arab Emirates"}
]
}
This is a list of my own class CountryWSType, I have several classes like this and need a way that can get the list of any type of them.
I've tried parse it like a list:
List<MyClass> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));
Also trying creating an own list type:
public class ListWSType<T> implements List<T>{
private List<T> listaInterna;
//implents methods
}
But I always get a JsonMappingException and I have no more ideas of how to do it.
I hope someone can help me.
As some people ask here is the class i was trying to parse from a JSON:
@XmlRootElement(name="country")
@XmlType(propOrder={"code", "nombre", "name"})
public class CountryWSType {
/** Código ISO */
@XmlElement(name="code")
public String code;
/** Nombre en español */
@XmlElement(name="nombre")
public String nombre;
/** Nombre en inglés */
@XmlElement(name="name")
public String name;
/** Constructor sin parámetros. No inicializa nada...
* Está para que funcione el marshall/unmarshall.
*/
public CountryWSType() {}
}
Also notice than when i put MyClass it means CountryWSType class, sorry for the missunderstood.
[{...}, {...}, ...]. You have{"country": [{...}, {...}, ...]}. That's not a list, it's an object with a fieldcountrywhich is a list.