How to convert list data into json in java

前端 未结 7 1183
天涯浪人
天涯浪人 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 <T> List<T> convertToList(String jsonString, Class<T> 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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题