简体   繁体   中英

how to convert from list<object[]> to a specific a form

I'm working with spring boot and angular have an issue I'm selecting from two tables that make my query return list<object[]>

 @Query(value = "select count(*), p.des_prod from produit p, pack k,dis d WHERE p.cod_prod = k.cod_prod and k.cod_dist=d.cd_dist and adresse =:adresse group by p.des_prod", nativeQuery = true) List<Object> query(@Param("adresse") String adresse);

that query returns something like that

 [ [ 6, "article 1 " ], [ 8, "article2 " ]

but in the front (angular) I need it in a form like that

[{ name: "article 1 ", y: 6 }, { name: "article2 ", y: 8 }]
  1. Change return from List<Object> to List<Object[]> in your query method.

  2. Use Collectors toList(). Note: This is an example code.

     List<Object[]> result = spotifyRepository.query();// [["Shawn Mendes", "Senorita","canadian pop",99],... ], List<Object> resultList = result.stream().map(x -> { Spotify data= new Spotify(); data.artistName =x[0].toString(); data.trackName =x[1].toString(); data.genre =x[2].toString(); data.popularity =Integer.valueOf((int) x[3]); return data; }).collect(Collectors.toList());// use resultList in order to get this [{"artistName": "Shawn Mendes","trackName": "Senorita", "genre": "canadian pop","popularity": 99},...]

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