Ballon
Ballon

Reputation: 7204

How to add custom object in Json created with gson

I've created JSON which is a list of cars:

public class Car {

    @Expose
    public String producer;

    @Expose
    public String model;
    ....
}

and I create JSON with :

Gson gson = new Gson();
gson.toJson(car.getList());

But now I want to add extra field outside the list inside JSON.

Upvotes: 0

Views: 415

Answers (1)

Benjamin Caure
Benjamin Caure

Reputation: 2403

You should define another container class having the car list as attribute and the new field your talking about. Gson will serialize this container object and produce the JSON object you want.

Upvotes: 1

Related Questions