How to convert list data into json in java

前端 未结 7 1186
天涯浪人
天涯浪人 2020-11-30 06:57

I have a function which is returning Data as List in java class. Now as per my need, I have to convert it into Json Format.

Below is my fun

7条回答
  •  攒了一身酷
    2020-11-30 07:28

    You can use the following method which uses Jackson library

    public static  List convertToList(String jsonString, Class target) {
        if(StringUtils.isEmpty(jsonString)) return List.of();
    
            return new ObjectMapper().readValue(jsonString, new ObjectMapper().getTypeFactory().
                    constructCollectionType(List.class, target));
        } catch ( JsonProcessingException | JSONException e) {
            e.printStackTrace();
            return List.of();
        }
    }
    

提交回复
热议问题