1

To simplify I have json string with the structure:

{
  "route": {
    "bus-1": {
      "stations": [            
      ],
      "geo": [            
      ]
    },
    "bus-2": {
      "stations": [            
      ],
      "geo": [          
      ]
    }
  },
  "routesReverse": {
    "bus-1": {
      "stations": [           
      ],
      "geo": [            
      ]
    },
    "bus-2": {
      "stations": [            
      ],
      "geo": [
      ]
    }
  }
}

I'm trying to parse it using GSON:

public class MainJson {

    @SerializedName("route")
    @Expose
    public Routes route;
    @SerializedName("routesReverse")
    @Expose
    public Routes routesReverse;

    public Routes getRoute() {
        return route;
    }

    public Routes getRoutesReverse() {
        return routesReverse;
    }
}

I had created all models but and I have a question about this model:

public class Routes {

    @SerializedName("bus-1")
    @Expose
    BusStop busStop1;

    @SerializedName("bus-2")
    @Expose
    BusStop busStop2;

    public BusStop getBusStop1() {
        return busStop1;
    }

    public BusStop getBusStop2() {
        return busStop2;
    }
}

I don't like this approach to create BusStop object with annotation for each bus-route, I would like to create something like List<BusStop> because my json has not only 2 routes.

How to achieve that ?

VLeonovs
  • 2,193
  • 5
  • 24
  • 44

2 Answers2

2

Can you modify the structure of a json you receive? Because the simplest way would be to have an array named "bus" instead of multiple "bus-i" objects inside "route" json object. If you can't modify json, then I don't think any convenient solution inside GSON is available, since it maps object 1-to-1, even if you apply 'alternate' tag. Check out docs for @SerializedName annotation here.

  • If you can't modify json for some reason, I'd personally write some custom TypeAdapter for GSON (something like [there](http://stackoverflow.com/questions/6014674/gson-custom-deseralizer-for-one-variable-in-an-object)) to handle this problem. – Przemyslaw Jablonski Oct 22 '16 at 16:11
  • Unfortunately no, I can't modify, because of huge structure and GSON is not a goal, you can suggest any approach which you preffer – VLeonovs Oct 22 '16 at 16:25
0
//Each object is just a list of bus stops
public class MainJson {
    @Expose
    public List<BusStop> route;

    @Expose
    public List<BusStop> routesReverse;

    public List<BusStop> getRoute() {
        return route;
    }

    public List<BusStop> getRoutesReverse() {
        return routesReverse;
    }
}

public class BusStop {
    @Expose
    List<Object> stations;

    @Expose
    List<Object> geo;

    public List<Object> getStations() {
        return stations;
    }

    public List<Object> getGeo() {
        return geo;
    }
}

It's not clear what stations/geo contain, but since you used array notation, I assume they each contain a list of objects.

jlewkovich
  • 2,725
  • 2
  • 35
  • 49
  • With your models I have exception: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT – VLeonovs Oct 22 '16 at 16:18
  • @VLeonovs Please share the code that causes this exception, I'm assuming it's when you're trying to serialize/deserialize – jlewkovich Oct 22 '16 at 16:20
  • MainJson mainJson = gson.fromJson(jsonString, MainJson.class); after this I have this exception – VLeonovs Oct 22 '16 at 16:22
  • @VLeonovs So it looks like `route` and `routesReverse` are not actually arrays because they use the `{}` notation. If you want them to contain N amount of `BusStop`, you must mark their collection as an array with `[]`. If changing the JSON structure is off the table, then you will not be able to create a `List` because that can only be used when the object contains an array. – jlewkovich Oct 22 '16 at 16:27