休息服务 json 输出的格式问题

Formatting issues with rest service json output

我下面java代码returns输出如下

public String getAttributeMaster_2() throws JsonProcessingException {       
    Cluster cluster = couchBaseConnectionManager.openBucketOfActivCluster(CouchbaseBucket.RangePlan);
    Map<String,Object> attributeMap = new HashMap<String,Object>();
    attributeMap.put("Product_Brand", attributeMasterRepository.getProductBrand(cluster, CouchbaseBucket.RangePlan));   
    attributeMap.put("Product_Type", attributeMasterRepository.getProductType(cluster, CouchbaseBucket.RangePlan));
    attributeMap.put("Product_Event", attributeMasterRepository.getProductEvent(cluster, CouchbaseBucket.RangePlan));
    String attribute = attributeMap.toString();

    return attribute;

}

输出:

{"Product_Type"=[{"active":true,"description":"ACCESSORIES","id":1}, {"active":true,"description":"ALL IN ONES","id":2}], "Product_Brand"=[{"active":false,"description":"101 DALMATIANS","id":1}], "Product_Event"=[{"description":"BABY EVENT","id":2}, {"description":"ACTIVE EVENT","id":1}]}

预期输出:

{"Product_Type":[{"active":true,"description":"ACCESSORIES","id":1}, {"active":true,"description":"ALL IN ONES","id":2}], "Product_Brand":[{"active":false,"description":"101 DALMATIANS","id":1}], "Product_Event":[{"description":"BABY EVENT","id":2}, {"description":"ACTIVE EVENT","id":1}]}

问题: 我想用 :(冒号).

替换 =(等于) 符号

例如:Product_Type 为 "Product_Type"。 Product_Brand 和 Product_Event 也是如此。

如果有人可以帮助我实现这一点,请告诉我。

我是 java 编码新手。感谢您的回复。

谢谢,

除了生成由 attributeMap.toString() 返回的字符串表示之外,我没有看到 attributeMap 中的键添加任何值。

在这种情况下,您可以简单地转义并为键添加双引号,如下所示:

Map<String,Object> attributeMap = new HashMap<String,Object>();
attributeMap.put("\"Product_Brand\"", ...);
attributeMap.put("\"Product_Type\"", ...);
attributeMap.put("\"Product_Event\"", ...);

String attribute = attributeMap.toString();

添加引号很有效:

public String getAttributeMaster_2() throws JsonProcessingException {       
    Cluster cluster = couchBaseConnectionManager.openBucketOfActivCluster(CouchbaseBucket.RangePlan);
    Map<String,Object> attributeMap = new HashMap<String,Object>();
    attributeMap.put("\Product_Brand\"", attributeMasterRepository.getProductBrand(cluster, CouchbaseBucket.RangePlan));   
    attributeMap.put("\"Product_Type\"", attributeMasterRepository.getProductType(cluster, CouchbaseBucket.RangePlan));
    attributeMap.put("\"Product_Event\"", attributeMasterRepository.getProductEvent(cluster, CouchbaseBucket.RangePlan));
String attribute = attributeMap.toString();

return attribute;

}

您可以使用 json 库,例如 net.sf.json.JSONObject

 import net.sf.json.JSONObject;
 public String getAttributeMaster_2(){
    Cluster cluster = couchBaseConnectionManager.openBucketOfActivCluster(CouchbaseBucket.RangePlan);
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("Product_Brand", "");
    jsonObject.put("Product_Brand", attributeMasterRepository.getProductBrand(cluster, CouchbaseBucket.RangePlan));
    jsonObject.put("Product_Type", attributeMasterRepository.getProductType(cluster, CouchbaseBucket.RangePlan));
    jsonObject.put("Product_Event", attributeMasterRepository.getProductEvent(cluster, CouchbaseBucket.RangePlan));
    String attribute = jsonObject.toString();
}