简体   繁体   English

如何将JSON读取到java中的通用对象列表?

[英]How to read JSON to a list of generic objects in java?

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. 我必须从Web服务获取数据,我使用的是Jackson,但是我使用Gson也有同样的问题,我对单个对象没有任何问题,但是当我收到几个对象列表时,对我来说并不容易。

JSON received are like this: 收到的JSON是这样的:

{"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. 这是我自己的类CountryWSType的列表,我有几个这样的类,需要一种方法可以获得任何类型的列表。 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. 但我总是得到一个JsonMappingException ,我对如何做到这一点没有更多的想法。

I hope someone can help me. 我希望有一个人可以帮助我。

As some people ask here is the class i was trying to parse from a JSON: 正如有些人在这里问的那样是我试图用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. 还要注意的是,当我把MyClass放在它的意思是CountryWSType类时,对不起来了。

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. 如果你说你的json arrary元素可以是不同的类型,那么mapper将不知道要实例化哪个类的类,你需要在你的json中提供额外的属性来指示这一点,特别是如果json是外部的,这是一种丑陋的方法面对。 But if this is what you want than it is explained in this SO post. 但是,如果这是你想要的,而不是在这篇 SO帖子中解释的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM