3

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.

4
  • I'm guessing MyClass is your class backing the JSON objects? Maybe if we could see this class and the error stack trace it would be easier to find out what's the problem. Commented Jun 4, 2015 at 2:41
  • A List<MyClass> would be [{...}, {...}, ...]. You have {"country": [{...}, {...}, ...]}. That's not a list, it's an object with a field country which is a list. Commented Jun 4, 2015 at 3:16
  • You have, at the outermost layer, a JSON object, not a JSON array. This translates to a Map, not a List. Commented Jun 4, 2015 at 3:46
  • well, yes i think thats why i can't take the list like an array directly or something like this, i saw in other solutions that it will get it implemeting a class with an atribute named country as a list<CountryWSType>, but i need a way that will work also with other types, not only with CountryWSType. Dont know if i explain correctly... Commented Jun 4, 2015 at 3:48

1 Answer 1

0

May be this will help:

//first convert input string to json array
JSONObject jsonobject = new JSONObject(countriesAsJsonString);

JSONArray jsonArray = jsnobject.getJSONArray("countries");

//then get the type for list and parse using gson as
Type listType = new TypeToken<List<MyClass>>(){}.getType();
List<MyClass> countriesList = new Gson().fromJson(jsonArray, listType);

If you are saying your json arrary elements can be of different types, than the mapper will not know which type of class to instantiate and you will need to supply additional attribute in your json to indicate that, an ugly approach specially if the json is external facing. But if this is what you want than it is explained in this SO post.

Sign up to request clarification or add additional context in comments.

3 Comments

yes, its external facing. But the thing is i wont get diferent types in the same Json just need a solution that will work with different classes, for example i cant use an own class like this public class ListCountryWSType { private List<CountryWSType> countries; } because it will only work with CountryWSType and i need it for other types too.
Also checked your answer and I'm having problems with List<MyClass> countriesList = new Gson().fromJson(jsonArray, listType); i think its because JSONObject and JSONArray are classes from org.json and ´Gson().fromJson´ expect owns gson classes? Thanks for the answer anyway.
Well thank you, i change some of your code and it works perfectly, thats what i changed: List<MyClass> countriesList = new Gson().fromJson(jsonArray.toString(), listType); Thank you again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.