0

I have a JSON-string and use GSON to create objects of it. However, there appears to be an error because gson does not know List is supposed to be a list of Passengers. But I can not seem to make it work. I must tell GSON that it should retrieve Walkin containing Passengers.

 public static void main(String[] args) {
    String json = "{'price':278,'id':1459465,'passengers':[{'type':'student','price':99},{'type':'student','price':179}]}";
    Gson gson = new Gson();
    WalkIn walkinRequest = gson.fromJson(json, WalkIn.class);
    //I have also tried WalkIn walkinRequest = gson.fromJson(json, new TypeToken<WalkIn>(){}.getType());

    System.out.println("price: " + walkinRequest.price); 
    System.out.println("id: " + walkinRequest.id);
    Passenger p = (Passenger) walkinRequest.passengers.get(0); //Error
    PassengerOne p1 = (PassengerOne) p;
    System.out.println("Passenger - Type: " + p1.type + " price: " + p1.price);
 }

static class WalkIn {
    int price;
    int id;
    List passengers;
}

static interface Passenger {
    public List getContent();
}

static class PassengerOne implements Passenger{
    String type;
    int price;

    public List getContent() {
        List list = new ArrayList();
        list.add(type);
        list.add(price);
        return list;
    }   
}

static class PassengerTwo implements Passenger{
    String name;
    int age;

    public List getContent() {
        List list = new ArrayList();
        list.add(name);
        list.add(age);
        return list;
    }   
}

The output is:

 price: 278
 id: 1459465
 Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to testpack.GsonJson$Passenger
      at testpack.GsonJson.main(GsonJson.java:35)

I can not put

 List<Passenger> 

inside Walkin instead of List, because I use different types of Passenger objects in Walkin. Depends on what I want to use.

TorK
  • 567
  • 2
  • 10
  • 27
  • Never used GSON, but maybe `List passengers` or `Passenger[] passengers` in `WalkIn` ? See http://stackoverflow.com/questions/5554217/google-gson-deserialize-listclass-object-generic-type – NiziL Feb 13 '15 at 12:01
  • yes, it will work if I return List inside the JSON instead of just List. But the method is used for different types of passenger-objects... example PassengerOne { int price, String type} and PassengerTwo {String Name, int price } etc. So WalkIn MUST contain List. Later it is converted – TorK Feb 13 '15 at 12:06
  • converted to PassengerOne p = walkInRequest.passengers.get(0). Other times PassengerTwo is passed in the JSON. – TorK Feb 13 '15 at 12:08
  • Maybe this [question about polymorphism with GSON](http://stackoverflow.com/questions/5800433/polymorphism-with-gson) will help you :) – NiziL Feb 13 '15 at 12:11

1 Answers1

0

Just use 'Passenger' class as Gson array instead of List, refer below

import com.google.gson.annotations.SerializedName;

static class WalkIn {
    @SerializedName("price")
    int price;
    @SerializedName("id")
    int id;
    @SerializedName("passengers")
    Passenger[] passenger_list ;
}

static class Passenger {
    @SerializedName("type")
    String type;
    @SerializedName("price")
    int price;

}
Sino Raj
  • 6,431
  • 2
  • 22
  • 23
  • yes, it will work if I put List inside the WalkIn.class instead of just List. But the method is used for different types of passenger-objects... example PassengerOne { int price, String type} and PassengerTwo {String Name, int price } etc. So WalkIn MUST contain List. Later it is – TorK Feb 13 '15 at 12:34
  • I have edited my answer, please refer & I promise this works 100% sure. – Sino Raj Feb 13 '15 at 12:40
  • converted to PassengerOne p = walkInRequest.passengers.get(0). Other times PassengerTwo is passed in the JSON. – TorK Feb 13 '15 at 12:40
  • In 'Passenger[] passenger_list', you can get whole passengers array of values. just iterate passenger_list, you would get list of passengers details. I didn't understand why you are using 'PassengerOne' & 'PassengerTwo' classes. – Sino Raj Feb 13 '15 at 12:45
  • I have updated my question, take a look and come back to me :) – TorK Feb 13 '15 at 12:46
  • List data type is not applicable in JSON data, please refer this- 'http://www.tutorialspoint.com/json/json_data_types.htm'. And also your json string (String json = "{'price':278,'id':1459465,'passengers':[{'type':'student','price':99},{'type':'student','price':179}]}";) is only contains json array, int & string values. Then why you are using way. – Sino Raj Feb 13 '15 at 12:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70852/discussion-between-sino-raj-and-tork). – Sino Raj Feb 13 '15 at 12:53