0

I am trying to parse following JSON data using Retrofit { "message": false, "suggestions": false, "vehicle": { "parked": true, "uin": "15", "vin": "WBAEG1312MCB42267", "make": "Bmw", "model": "E8SERIES", "color": "Blue", "year": "1991", "package": "Premium", "options": "", "interior": "Color: Cream, Type:Leather", "exterior": "", "activity": "Parked", "username": "Dhruba Sarma", "timestamp": "04-Sep, 00:35", "latlng": { "lat": 12.899270164792, "lng": 77.646080134509 } } }

I have created my model classes as follows - VehicleModel.java

`public class VehicleModel {

@SerializedName("message")
@Expose
private Boolean message;
@SerializedName("suggestions")
@Expose
private Boolean suggestions;
@SerializedName("vehicle")
@Expose
private Vehicle vehicle;

public Boolean getMessage() {
    return message;
}

public Boolean getSuggestions() {
    return suggestions;
}

public Vehicle getVehicle() {
    return vehicle;
}

Vehicle.java `public class Vehicle {

@SerializedName("parked")
@Expose
private Boolean parked;
@SerializedName("uin")
@Expose
private String uin;
@SerializedName("vin")
@Expose
private String vin;
@SerializedName("make")
@Expose
private String make;
@SerializedName("model")
@Expose
private String model;
@SerializedName("color")
@Expose
private String color;
@SerializedName("year")
@Expose
private String year;
@SerializedName("package")
@Expose
private String _package;
@SerializedName("options")
@Expose
private String options;
@SerializedName("interior")
@Expose
private String interior;
@SerializedName("exterior")
@Expose
private String exterior;
@SerializedName("activity")
@Expose
private String activity;
@SerializedName("username")
@Expose
private String username;
@SerializedName("timestamp")
@Expose
private String timestamp;
@SerializedName("latlng")
@Expose
private LatLng latlng;



public Boolean getParked() {
    return parked;
}

public String getUin() {
    return uin;
}

public String getVin() {
    return vin;
}

public String getMake() {
    return make;
}

public String getModel() {
    return model;
}

public String getColor() {
    return color;
}

public String getYear() {
    return year;
}

public String getPackage() {
    return _package;
}

public String getOptions() {
    return options;
}

public String getInterior() {
    return interior;
}

public String getExterior() {
    return exterior;
}

public String getActivity() {
    return activity;
}

public String getUsername() {
    return username;
}

public String getTimestamp() {
    return timestamp;
}

public LatLng getLatlng() {
    return latlng;
}

}`

And finally LatLng.java `public class LatLng {

@SerializedName("lat")
@Expose
private Double lat;
@SerializedName("lng")
@Expose
private Double lng;

public Double getLat() {
    return lat;
}

public Double getLng() {
    return lng;
}

I even tried using jsonschema2pojo for reference. But i still get the below error com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT

Please suggest what am i doing wrong ?

EDIT- Here is how i am parsing JSON

Retrofit retrofit = new Retrofit.Builder().baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create()).build();

    VehicleRequestInterface request = retrofit.create(VehicleRequestInterface.class);
    Call<VehicleJSONResponse> call = request.getVehicleJSON(url);
    call.enqueue(new Callback<VehicleJSONResponse>() {
        @Override
        public void onResponse(Call<VehicleJSONResponse> call, Response<VehicleJSONResponse> response) {

            VehicleJSONResponse jsonResponse = response.body();
            vehicleData = new ArrayList<>(Arrays.asList(jsonResponse.getVehicle()));
sumesh
  • 2,179
  • 2
  • 21
  • 37

1 Answers1

0

Declare your vehicleData variable like this .

ArrayList<VehicleModel> vehicleData = new ArrayList<VehicleModel>();

And then in onResponse rather than

vehicleData = new ArrayList<>(Arrays.asList(jsonResponse.getVehicle()));

add like this way

vehicleData.add(jsonResponse.getVehicle())

The problem is you have no arrays in your json

Mithun Sarker Shuvro
  • 3,902
  • 6
  • 32
  • 64
  • now i am getting NullPointerException when i try to retrieve the data. `vehicleData.get(0).getVehicle().getLatlng().getLat();` – sumesh Sep 13 '16 at 18:14
  • Can you read the other value like vehicleData.get(0).getVehicle().getMake() ; – Mithun Sarker Shuvro Sep 13 '16 at 18:28
  • Nope. I tried it, but same result. I have also checked that `vehicleData` is not null. Not sure what the issue is here. – sumesh Sep 13 '16 at 18:29
  • `vehicleData.get(0).getVehicle()` is null. Not sure why !! Could u help – sumesh Sep 14 '16 at 06:35
  • debug vehicleData , lets check if u get something – Mithun Sarker Shuvro Sep 14 '16 at 17:09
  • i am getting `vehicleData={VehicleModel@6641}` `message=false` `suggestions=false` `vehicle=null` What next ?? – sumesh Sep 14 '16 at 18:27
  • try calling your api in postman , just check your api response correctly – Mithun Sarker Shuvro Sep 14 '16 at 18:31
  • i have already checked it on postman. The response is as expected. For your reference VehicleJSONResponse.jave looks like this `public class VehicleJSONResponse { private VehicleModel vehicle; public VehicleModel getVehicleJSONResponse() { return vehicle; } }` FYI i am now retrieving the data like this `vehicleData.getVehicle().getColor()` – sumesh Sep 14 '16 at 18:34