我有以下课程
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
}
我建立了GSON跟随
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
String jsonG = gson.toJson(OrderShoppingCart.getOrders());
// OrderShoppingCart.getOrders()返回所有属性
我得到以下
[{"id":"525b207b16b1e9ca33000143","selections":[],"quantity":1}]
但我需要这个
{items:[{"id":"525b207b16b1e9ca33000143","selections":[],"quantity":1}]}
如何添加我缺少的内容?
答案 0 :(得分:2)
尝试创建一个JsonObject并添加一个名为items
的成员并为您的Json对象赋值。
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
JsonObject jObj = new JsonObject();
jObj.add("items", gson.toJson(OrderShoppingCart.getOrders()));
String jsonG = jObj.toString();