简体   繁体   中英

How to map Json (from procedure) to Java object

I have the following SP (SQL server) that return a Json output.

BEGIN
SET @jsonOutput = (
SELECT 
    Program.Name AS ProgramName,
    ProgramOwner.FirstName AS OwnerFirstName,
FROM ProgramOwner, Program
WHERE Program.Id = ProgramOwner.ProgramOwner2Program
FOR JSON PATH,WITHOUT_ARRAY_WRAPPER)

I would like to map the return Json output to a List of ProgramDto via modelMapper. Not sure hot to do that since the return values from call.execute is an Object.

Something like this:

SimpleJdbcCall call = new 
SimpleJdbcCall(jdbcTemplate).withProcedureName(programProc).declareParameters(
    new SqlOutParameter("jsonOutput",  Types.VARCHAR));
    Map<String,Object>out = call.execute(new MapSqlParameterSource());
if(out.size()>0) {
    // Only to show what I am trying to do 
    Type rootType = new TypeToken<List<ProgramDto>>() {}.getType();
    modelMapper.map(out.get("jsonOutput"),rootType );
}

Thank you

As I understood you are trying to get a list of object from You can use Jackson api

Like this

say for example your json is in variable named jsonData, then you can get the object you need like below.

ObjectMapper mapper = new ObjectMapper();
List<Type> myList = Arrays.asList(mapper.readValue(jsonData, Type[].class));

You can also find more examples here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM