Dan
Dan

Reputation: 147

Deserializing an array of strings using Rest Template in Java

I am making an api call and the json is returning as an array of strings. How do I deserialize an array of strings? They do not include a key of any kind so the usual way I would deserialize is not working. This is using the league of legends api for game data. https://developer.riotgames.com/

JSON Data returned by api:

[
    "NA1_3946470802",
    "NA1_3946414706",
    "NA1_3945276842",
    "NA1_3945236107"
]

Code snippets:

public class MatchList {

    public List<String> matches;

    public MatchList(List<String> matches) {}
}

@RestController
@RequestMapping("/api")
public class SummonerController {
  private RestTemplate restTemplate = new RestTemplate();

    @RequestMapping("/summoner/test/{summonerName}")
    public MatchList getSummonerInfoTest(@PathVariable String summonerName){

        MatchList matchstest = restTemplate.getForObject("https://americas.api.riotgames" +
                        ".com/lol/match/v5/matches/by-puuid/"+ summoner.getPuuid()+ "/ids? 
start=0&count=100&api_key="+apikey,
                MatchList.class);

        return matchstest;
    }
}

I have tried using :

private List<String> matches;
public List<String> matches;
public ArrayList<String> matches;
public String[] matches;

Upvotes: 0

Views: 386

Answers (1)

Suraj
Suraj

Reputation: 3137

Your api is returning array. So you have to give the class type as array.

String[] matchstest = restTemplate.getForObject("https://americas.api.riotgames" +
                        ".com/lol/match/v5/matches/by-puuid/"+ summoner.getPuuid()+ "/ids? 
start=0&count=100&api_key="+apikey,
                String[].class);

You can return this array as list if you want. You can use Arrays.asList(matchstest)

Upvotes: 1

Related Questions