在模型响应中使用转义序列传递String时,响应对象中会出现其他转义序列

时间:2016-06-17 12:49:24

标签: java json model resteasy

CartModel:

public class GeneralCartModel implements Serializable {
    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 8186401416931298029L;

    /** active_cart.participantId. */
    private UUID participantId;

    /** active_cart.storeId. */
    private Integer storeId;

    /** isConsolidationNeeded. */
    private Boolean isConsolidationNeeded;

    /** Cart entity. */
    private Cart cartEntity;

    // With Getters and setters and to-String.

}

购物车(坚持cassandra的实体类):

@Table(keyspace = "keyspace1", name = "cart")

public class ActiveCart implements Serializable {

    /**
     * the serialVersionUID.
     */
    @Transient
    private static final long serialVersionUID = 5893895498496519909L;

    /** active_cart.total_product. */
    @Column(name = "total_product")
    private Integer totalProduct;

    /** active_cart.billing_address. */
    @Column(name = "billing_address")
    private String billingAddress;
    // With getters and setters
}

以下代码的REST呼叫:

JSONObject billingAddress = new JSONObject();
billingAddress.put("organizationName", "organizationName");
billingAddress.put("attentionTo", "attentionTo");
billingAddress.put("department", "department");

/**set billing address in entity.*/
CartModel cartModel=new CartModel;
cartModel.getcartEntity.setBillingAddress(billingAddress.toString());

/** build response.*/
final Response response = Response.build().entity(generalCartModel);
response.setStatus(Status.OK.getStatusCode());
return response;

响应:

{  
   "participantId":null,
   "storeId":null,
   "isConsolidationNeeded":null,
   "activeCartEntity":{  
      "totalProduct":3,
      "billingAddress":**"{\"country\":\"US\",\"organizationName\":\"UNIV OF CHICAGO (SMW)\",\"street3\":\"\",\"street4\":\"\",\"city\":\"CHICAGO\",\"street\":\"1225 E 60TH ST\",\"street5\":\"\",\"postalcode\":\"60637\",\"attentionTo\":\"CENTRAL PROCURMENT\",\"state\":\"\",\"department\":\"\",\"buildingRoom\":\"\"}"**
   }
}

我希望得到回应:

{  
   "participantId":null,
   "storeId":null,
   "isConsolidationNeeded":null,
   "activeCartEntity":{  
      "totalProduct":3,
      "billingAddress":**"{"country":"US","organizationName":"UNIV OF CHICAGO (SMW)","street3":"","street4":"","city":"CHICAGO","street":"1225 E 60TH ST","street5":"","postalcode":"60637","attentionTo":"CENTRAL PROCURMENT","state":"","department":"","buildingRoom":""}"**
   }
}

我该怎么办?

1 个答案:

答案 0 :(得分:0)

即使使用GSON,我也遇到了同样的问题。所以,我修改了代码,将其作为JSON对象内的地图发送给它。它按预期工作。 (即)我已将计费地址添加为"地图"而不是JSONObject。