简体   繁体   English

如何解析JsonObject到列表 <MyClass> 使用gson

[英]How to parse JsonObject to List<MyClass> using gson

I need a list of objects and I'm having problems to get it. 我需要对象列表,但在获取它时遇到了问题。 I'm pretty new using this so someone who can help me? 我是新手,所以有人可以帮助我吗?

I'm using volley to get a jsonObject and then I need convert it(I saw the best wa to do this is with gson). 我正在使用凌空来获取jsonObject,然后需要将其转换(我看到的最好的方法是使用gson)。 Below you can see how looks an example of my json. 在下面,您可以看到我的json示例的外观。

{
network: {
    company: "JCDecaux",
    href: "/v2/networks/dublinbikes",
    id: "dublinbikes",
    location: {
        city: "Dublin",
        country: "IE",
        latitude: 53.3498053,
        longitude: -6.2603097
    },
    name: "dublinbikes",
    stations: [
        {
            empty_slots: 37,
            extra: {
                address: "Fitzwilliam Square East",
                bonus: false,
                connected: "1",
                last_update: "1434047944",
                open: true,
                slots: 40,
                ticket: true,
                uid: 89
            },
            free_bikes: 3,
            id: "153ff4dfb7bd8912ef91c10849129c2e",
            latitude: 53.335211,
            longitude: -6.2509,
            name: "Fitzwilliam Square East",
            timestamp: "2015-06-11T18:41:31.11Z"
        },
        {
            empty_slots: 0,
            extra: {
                address: "Portobello Harbour",
                bonus: false,
                connected: "1",
                last_update: "1434047764",
                open: true,
                slots: 30,
                ticket: false,
                uid: 34
            },
            free_bikes: 30,
            id: "3c0cfd547a142bb651280991a412bcbe",
            latitude: 53.330362,
            longitude: -6.265163,
            name: "Portobello Harbour",
            timestamp: "2015-06-11T18:41:31.15Z"
        },

... ect ...等

Station class 车站班

public class Station {
    public Station(){}

    public String StationName;
    public String Distance;
    public String Slots;
    public String Bikes;
    public String LastUpdate;

    //Getters and Setters ...
}

Below you can see what I have done so far.. 在下面您可以看到我到目前为止所做的一切。

 //stations
 JSONObject jsonNetwork = new JSONObject(response.getString("network"));

   Type listType = new TypeToken<ArrayList<Station>>() {
                }.getType();
   List<Station> yourClassList = new Gson().fromJson(jsonNetwork, listType);

But I don't know how to parse all this and avoid the data I dont need and also the function Gson().fromJson needs and JsonArray and what I have is a JsonObject 但是我不知道如何解析所有这些内容,避免我不需要的数据以及函数Gson().fromJson需要和JsonArray,而我拥有的是JsonObject

Thanks for your time! 谢谢你的时间!

If all that you are looking for are the stations then I think you should do the following: 如果您要寻找的只是车站,那么我认为您应该执行以下操作:

//stations
    JSONObject jsonNetwork = new JSONObject(response.getString("network"));
    JSONArray stationsArray = jsonNetwork.getJSONArray("stations");

Now, you should pass this stationsArray variable to the fromJson method. 现在,您应该将此stationsArray变量传递给fromJson方法。 Also, the variable names of your Station class should be equal to the key names that you want to extract from your json. 另外,Station类的变量名称应等于要从json中提取的键名。 There is a good example in the following link: 以下链接中有一个很好的例子:

http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/ http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

Use below code 使用以下代码

Gson gson = new Gson();
YourClass obj = gson.fromJson(jsonObject.toString(), YourClass.class);

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

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