简体   繁体   English

如何将 Json(从过程)映射到 Java 对象

[英]How to map Json (from procedure) to Java object

I have the following SP (SQL server) that return a Json output.我有以下返回 Json 输出的 SP(SQL 服务器)。

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.我想通过modelMapper将返回的Json输出映射到ProgramDto列表。 Not sure hot to do that since the return values from call.execute is an Object.因为 call.execute 的返回值是一个对象,所以不确定这样做。

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据我了解,您正在尝试从您可以使用Jackson api获取对象列表

Like this像这样

say for example your json is in variable named jsonData, then you can get the object you need like below.比如说你的 json 在名为 jsonData 的变量中,那么你可以得到你需要的对象,如下所示。

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

You can also find more examples here您还可以在此处找到更多示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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