简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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