将枚举类转换为Json并在Java中添加密钥

将枚举类转换为Json并在Java中添加密钥,java,json,Java,Json,我一直在尝试将包含一些值的枚举类转换为Json对象 到目前为止,我已经做了以下工作: public class ExpertiseService { ObjectMapper mapper = new ObjectMapper(); JSONObject obj = new JSONObject(); public String getAllExpertise() throws JsonProcessingException, JSONException{

我一直在尝试将包含一些值的枚举类转换为Json对象

到目前为止,我已经做了以下工作:

public class ExpertiseService {

    ObjectMapper mapper = new ObjectMapper();
     JSONObject obj = new JSONObject();
    public String getAllExpertise() throws JsonProcessingException, JSONException{
        ExpertiseCategory[] expertiseArray = ExpertiseCategory.values();
        obj.put("expertise",Arrays.toString(expertiseArray));
        return obj.toString();
    }

    public static void main(String args[]){
        ExpertiseService obj = new ExpertiseService();
        try {
            System.out.println(obj.getAllExpertise());
        } catch (JsonProcessingException | JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
输出:

{"expertise":"[SALES, SOFTWARE_DEVELOPMENT, BUSINESS_OPERATIONS]"}
但我希望输出结果是

{"expertise":["SALES","SOFTWARE_DEVELOPMENT", "BUSINESS_OPERATIONS"]}

您有
数组。toString
给您一个字符串,但是您想要一个JSON数组。您可以使用
映射器创建JSON数组。我试过了,但在每个值的开头和结尾都给了我“\”。请告诉我们您到底尝试了什么。我使用ObjectMapper insead这个obj.put(“专家”,Arrays.toString(expertiseArray));我做了这个obj.put(“专家”,objectMapper.writeValueAsString(expertiseArray));应该有一个由
mapper
提供的方法“创建数组”(或类似方法)。请尝试理解字符串和实际数组对象之间的区别。