2014-02-11 3 views
0

나는 모든 부가 요소 GSON에 JSON에 속성을 추가하는 방법은 무엇입니까?

를 반환) 나는 GSON이 (

Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); 
       String jsonG = gson.toJson(OrderShoppingCart.getOrders()); 

//OrderShoppingCart.getOrders을 다음 내장 다음과 같은 클래스를

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 
} 

을하고 난 다음

[{"id":"525b207b16b1e9ca33000143","selections":[],"quantity":1}] 
있어

하지만이 번호가 필요합니다.

{items:[{"id":"525b207b16b1e9ca33000143","selections":[],"quantity":1}]} 

내가 누락 된 부분을 어떻게 추가 할 수 있습니까?

답변

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(); 
+0

Gracias, 작동 여부 – cc69cc

관련 문제