Converting JSON data to Java object

后端 未结 12 1136
春和景丽
春和景丽 2020-11-21 05:11

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson()

12条回答
  •  眼角桃花
    2020-11-21 05:42

    The easiest way is that you can use this softconvertvalue method which is a custom method in which you can convert jsonData into your specific Dto class.

    Dto response = softConvertValue(jsonData, Dto.class);
    
    
    public static  T softConvertValue(Object fromValue, Class toValueType) 
    {
        ObjectMapper objMapper = new ObjectMapper();
        return objMapper
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .convertValue(fromValue, toValueType);
    }
    

提交回复
热议问题