I have the following class
public class OrderShoppingCart {
@Expose
@SerializedName("id")
private String _id;
@Expose
@SerializedName("quantity")
private int _quantity;
private String _description;
private String _name;
private int _price;
@Expose
@SerializedName("selections")
private List<SelectionShoppingCart> _selections;
public OrderShoppingCart(String id, int quantity, String description, String name,int price, List<SelectionShoppingCart> selections) {
_id = id;
_quantity = Integer.valueOf(quantity);
_description = description;
_name = name;
_price = price;
_selections = selections;
}
//HERE GET AND SETTER
}
I built the GSON follows
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
String jsonG = gson.toJson(OrderShoppingCart.getOrders());
//OrderShoppingCart.getOrders() return all atributes
And I got the following
[{"id":"525b207b16b1e9ca33000143","selections":[],"quantity":1}]
but I need this
{items:[{"id":"525b207b16b1e9ca33000143","selections":[],"quantity":1}]}
How I can add what I'm missing?
Try to create a JsonObject and add a member with name items
and value your Json object.
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
JsonObject jObj = new JsonObject();
jObj.add("items", gson.toJson(OrderShoppingCart.getOrders()));
String jsonG = jObj.toString();
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.